1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-29 11:02:01 +00:00

Add a faster seq-uniq for lists

* lisp/emacs-lisp/seq.el (seq-uniq): Add a faster method for lists
(bug#57079).
This commit is contained in:
Lars Ingebrigtsen 2022-08-09 19:18:55 +02:00
parent b92e888758
commit 5269842833
2 changed files with 30 additions and 0 deletions

View File

@ -455,6 +455,23 @@ TESTFN is used to compare elements, or `equal' if TESTFN is nil."
(setq result (cons elt result))))
(nreverse result)))
(cl-defmethod seq-uniq ((sequence list) &optional testfn)
(let ((result nil))
(if (not testfn)
;; Fast path.
(while sequence
(unless (member (car sequence) result)
(push (car sequence) result))
(pop sequence))
;; Slower path.
(while sequence
(unless (seq-find (lambda (elem)
(funcall testfn elem (car sequence)))
result)
(push (car sequence) result))
(pop sequence)))
(nreverse result)))
(cl-defgeneric seq-mapcat (function sequence &optional type)
"Concatenate the result of applying FUNCTION to each element of SEQUENCE.
The result is a sequence of type TYPE, or a list if TYPE is nil."

View File

@ -559,5 +559,18 @@ Evaluate BODY for each created sequence.
(should (equal (seq-split seq 3)
'("012" "345" "678" "9")))))
(ert-deftest test-seq-uniq-list ()
(let ((list '(1 2 3)))
(should (equal (seq-uniq (append list list)) '(1 2 3))))
(let ((list '(1 2 3 2 1)))
(should (equal (seq-uniq list) '(1 2 3))))
(let ((list (list (substring "1")
(substring "2")
(substring "3")
(substring "2")
(substring "1"))))
(should (equal (seq-uniq list) '("1" "2" "3")))
(should (equal (seq-uniq list #'eq) '("1" "2" "3" "2" "1")))))
(provide 'seq-tests)
;;; seq-tests.el ends here