From 0cf0a2b98671671bb7639a17639ef2552b772cbe Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Fri, 17 Sep 2021 10:35:13 +0200 Subject: 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. --- lisp/emacs-lisp/seq.el | 11 +++++++++++ lisp/emacs-lisp/shortdoc.el | 2 ++ 2 files changed, 13 insertions(+) (limited to 'lisp/emacs-lisp') diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index f0dc283f57d..b7dcde87f41 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -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. diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index adee6be379d..3e0d5aef022 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -809,6 +809,8 @@ There can be any number of :example/:result elements." :eval (seq-remove #'numberp '(1 2 c d 5))) (seq-group-by :eval (seq-group-by #'cl-plusp '(-1 2 3 -4 -5 6))) + (seq-union + :eval (seq-union '(1 2 3) '(3 5))) (seq-difference :eval (seq-difference '(1 2 3) '(2 3 4))) (seq-intersection -- cgit v1.2.3