1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-05 22:20:24 -08:00

Add new sequence function 'seq-union'

* lisp/emacs-lisp/seq.el (seq-union): New function.
* doc/lispref/sequences.texi (Sequence Functions):
* lisp/emacs-lisp/shortdoc.el (sequence): Document above new
function.
* test/lisp/emacs-lisp/seq-tests.el (test-seq-union): New test.
This commit is contained in:
Stefan Kangas 2021-09-17 10:35:13 +02:00
parent fc10e7fe5f
commit 0cf0a2b986
5 changed files with 66 additions and 0 deletions

View file

@ -467,6 +467,17 @@ negative integer or 0, nil is returned."
(setq sequence (seq-drop sequence n)))
(nreverse result))))
(cl-defgeneric seq-union (sequence1 sequence2 &optional testfn)
"Return a list of all elements that appear in either SEQUENCE1 or SEQUENCE2.
Equality is defined by TESTFN if non-nil or by `equal' if nil."
(let ((accum (lambda (acc elt)
(if (seq-contains-p acc elt testfn)
acc
(cons elt acc)))))
(seq-reverse
(seq-reduce accum sequence2
(seq-reduce accum sequence1 '())))))
;;;###autoload
(cl-defgeneric seq-intersection (sequence1 sequence2 &optional testfn)
"Return a list of the elements that appear in both SEQUENCE1 and SEQUENCE2.