diff options
author | Nicolas Petton <nicolas@petton.fr> | 2019-03-20 21:44:01 +0100 |
---|---|---|
committer | Nicolas Petton <nicolas@petton.fr> | 2019-03-21 21:08:28 +0100 |
commit | 287cc58f39e9ca8f9ef31b31556f50c25feadaea (patch) | |
tree | 493af4f9d264395bba72ae15f0afa2162e135839 /lisp/emacs-lisp/seq.el | |
parent | 093d3e78d21d3d6c718997368ef4b31f9884401c (diff) | |
download | emacs-287cc58f39e9ca8f9ef31b31556f50c25feadaea.tar.gz emacs-287cc58f39e9ca8f9ef31b31556f50c25feadaea.tar.bz2 emacs-287cc58f39e9ca8f9ef31b31556f50c25feadaea.zip |
New seq-contains-p predicate (Bug#34852)
* lisp/emacs-lisp/seq.el (seq-contains-p): New predicate function. It
is a replacement for seq-contains which cannot be used as a predicate
when a sequence contains nil values as it returns the element found.
(seq-contains): Make obsolete.
* test/lisp/emacs-lisp/seq-tests.el (test-seq-contains-p):
(test-seq-intersection-with-nil, test-seq-set-equal-p-with-nil,
test-difference-with-nil): Add regression tests.
* doc/lispref/sequences.texi (Sequence Functions): Document
seq-contains-p.
Diffstat (limited to 'lisp/emacs-lisp/seq.el')
-rw-r--r-- | lisp/emacs-lisp/seq.el | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 4a811d78955..0b99b663ddc 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -356,6 +356,7 @@ found or not." count)) (cl-defgeneric seq-contains (sequence elt &optional testfn) + (declare (obsolete seq-contains-p "27.1")) "Return the first element in SEQUENCE that is equal to ELT. Equality is defined by TESTFN if non-nil or by `equal' if nil." (seq-some (lambda (e) @@ -363,11 +364,20 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." e)) sequence)) +(cl-defgeneric seq-contains-p (sequence elt &optional testfn) + "Return non-nil if SEQUENCE contains an element equal to ELT. +Equality is defined by TESTFN if non-nil or by `equal' if nil." + (catch 'seq--break + (seq-doseq (e sequence) + (when (funcall (or testfn #'equal) e elt) + (throw 'seq--break t))) + nil)) + (cl-defgeneric seq-set-equal-p (sequence1 sequence2 &optional testfn) "Return non-nil if SEQUENCE1 and SEQUENCE2 contain the same elements, regardless of order. Equality is defined by TESTFN if non-nil or by `equal' if nil." - (and (seq-every-p (lambda (item1) (seq-contains sequence2 item1 testfn)) sequence1) - (seq-every-p (lambda (item2) (seq-contains sequence1 item2 testfn)) sequence2))) + (and (seq-every-p (lambda (item1) (seq-contains-p sequence2 item1 testfn)) sequence1) + (seq-every-p (lambda (item2) (seq-contains-p sequence1 item2 testfn)) sequence2))) (cl-defgeneric seq-position (sequence elt &optional testfn) "Return the index of the first element in SEQUENCE that is equal to ELT. @@ -385,7 +395,7 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." TESTFN is used to compare elements, or `equal' if TESTFN is nil." (let ((result '())) (seq-doseq (elt sequence) - (unless (seq-contains result elt testfn) + (unless (seq-contains-p result elt testfn) (setq result (cons elt result)))) (nreverse result))) @@ -410,7 +420,7 @@ negative integer or 0, nil is returned." "Return a list of the elements that appear in both SEQUENCE1 and SEQUENCE2. Equality is defined by TESTFN if non-nil or by `equal' if nil." (seq-reduce (lambda (acc elt) - (if (seq-contains sequence2 elt testfn) + (if (seq-contains-p sequence2 elt testfn) (cons elt acc) acc)) (seq-reverse sequence1) @@ -420,7 +430,7 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." "Return a list of the elements that appear in SEQUENCE1 but not in SEQUENCE2. Equality is defined by TESTFN if non-nil or by `equal' if nil." (seq-reduce (lambda (acc elt) - (if (not (seq-contains sequence2 elt testfn)) + (if (not (seq-contains-p sequence2 elt testfn)) (cons elt acc) acc)) (seq-reverse sequence1) |