unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union')
@ 2021-09-17 11:13 Štěpán Němec
  2021-09-17 12:18 ` Stefan Kangas
  2021-09-17 12:25 ` Stefan Monnier
  0 siblings, 2 replies; 3+ messages in thread
From: Štěpán Němec @ 2021-09-17 11:13 UTC (permalink / raw)
  To: emacs-devel


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



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union')
  2021-09-17 11:13 seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union') Štěpán Němec
@ 2021-09-17 12:18 ` Stefan Kangas
  2021-09-17 12:25 ` Stefan Monnier
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Kangas @ 2021-09-17 12:18 UTC (permalink / raw)
  To: Štěpán Němec, emacs-devel

Štěpán Němec <stepnem@gmail.com> writes:

> +(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'.

I have pushed a fix to make it use `nreverse' instead.  This is faster,
and in line with e.g. seq-partition, seq-uniq, etc.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union')
  2021-09-17 11:13 seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union') Štěpán Němec
  2021-09-17 12:18 ` Stefan Kangas
@ 2021-09-17 12:25 ` Stefan Monnier
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Monnier @ 2021-09-17 12:25 UTC (permalink / raw)
  To: Štěpán Němec; +Cc: emacs-devel

> `seq-reverse' seems gratuitous here WRT the purpose of `seq-union'.
>
> Given that this is a set operation,

As the name indicates, this is a *seq*uence operation, not
a set operation.  I'd welcome a `set.el` which provides proper support
for sets.


        Stefan




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-17 12:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-17 11:13 seq-union calling seq-reverse? (Emacs master 0cf0a2b98671: Add new sequence function 'seq-union') Štěpán Němec
2021-09-17 12:18 ` Stefan Kangas
2021-09-17 12:25 ` Stefan Monnier

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).