emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new seque


From: Štěpán Němec
Subject: seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union')
Date: Fri, 17 Sep 2021 13:13:28 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index f0dc283f57d5..b7dcde87f412 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -467,6 +467,17 @@ seq-partition
         (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 '())))))
+

`seq-reverse' seems gratuitous here WRT the purpose of `seq-union'.

Given that this is a set operation, it makes more sense not to waste
resources on element order.

(
For comparison, both Common Lisp (n)union [1] and SRFI-1 lset-union [2]
leave the element order unspecified, and e.g. in SBCL:

* (union '(a b c) '(f a d))
(C B F A D)

[1] http://www.lispworks.com/documentation/HyperSpec/Body/f_unionc.htm

[2] https://srfi.schemers.org/srfi-1/srfi-1.html
)

-- 
Štěpán



reply via email to

[Prev in Thread] Current Thread [Next in Thread]