unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Štěpán Němec" <stepnem@gmail.com>
To: emacs-devel@gnu.org
Subject: seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union')
Date: Fri, 17 Sep 2021 13:13:28 +0200	[thread overview]
Message-ID: <20210917131328+0200.462267-stepnem@gmail.com> (raw)


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	other threads:[~2021-09-17 11:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-17 11:13 Štěpán Němec [this message]
2021-09-17 12:18 ` seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union') Stefan Kangas
2021-09-17 12:25 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210917131328+0200.462267-stepnem@gmail.com \
    --to=stepnem@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).