From: Nicolas Petton <nicolas@petton.fr>
To: "Basil L. Contovounesios" <contovob@tcd.ie>
Cc: Michael Heerdegen <michael_heerdegen@web.de>,
"Miguel V. S. Frasson" <mvsfrasson@gmail.com>,
34852@debbugs.gnu.org, Stefan Monnier <monnier@iro.umontreal.ca>
Subject: bug#34852: 26.1; seq-intersection ignores nil as element
Date: Wed, 20 Mar 2019 21:51:30 +0100 [thread overview]
Message-ID: <87r2b1nub1.fsf@petton.fr> (raw)
In-Reply-To: <87h8c0mm74.fsf@petton.fr>
[-- Attachment #1.1: Type: text/plain, Size: 33 bytes --]
Hi,
Here's a patch for master.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-New-seq-contains-p-predicate-Bug-34852.patch --]
[-- Type: text/x-patch, Size: 6097 bytes --]
From 985aa028f05a418dddfd6394b739f42c32b01345 Mon Sep 17 00:00:00 2001
From: Nicolas Petton <nicolas@petton.fr>
Date: Wed, 20 Mar 2019 21:44:01 +0100
Subject: [PATCH] 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.
---
doc/lispref/sequences.texi | 9 +++++----
lisp/emacs-lisp/seq.el | 20 +++++++++++++++-----
test/lisp/emacs-lisp/seq-tests.el | 21 +++++++++++++++++++++
3 files changed, 41 insertions(+), 9 deletions(-)
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 0c3c4e3b28..a7f270c068 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -782,10 +782,11 @@ Sequence Functions
@end defun
-@defun seq-contains sequence elt &optional function
- This function returns the first element in @var{sequence} that is equal to
-@var{elt}. If the optional argument @var{function} is non-@code{nil},
-it is a function of two arguments to use instead of the default @code{equal}.
+@defun seq-contains-p sequence elt &optional function
+ This function returns non-@code{nil} if at least one element in
+@var{sequence} is equal to @var{elt}. If the optional argument
+@var{function} is non-@code{nil}, it is a function of two arguments to
+use instead of the default @code{equal}.
@example
@group
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index 4a811d7895..39c93e25ed 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -356,6 +356,7 @@ seq-sort-by
count))
(cl-defgeneric seq-contains (sequence elt &optional testfn)
+ (declare (obsolete "Use `seq-contains-p' instead." "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 @@ seq-sort-by
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 @@ seq-sort-by
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 @@ seq-sort-by
"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 @@ seq-sort-by
"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)
diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el
index d8f00cfea4..6522def423 100644
--- a/test/lisp/emacs-lisp/seq-tests.el
+++ b/test/lisp/emacs-lisp/seq-tests.el
@@ -185,6 +185,14 @@ test-sequences-oddp
(with-test-sequences (seq '(3 4 5 6))
(should (= 5 (seq-contains seq 5)))))
+(ert-deftest test-seq-contains-p ()
+ (with-test-sequences (seq '(3 4 5 6))
+ (should (eq (seq-contains-p seq 3) t))
+ (should-not (seq-contains-p seq 7)))
+ (with-test-sequences (seq '())
+ (should-not (seq-contains-p seq 3))
+ (should-not (seq-contains-p seq nil))))
+
(ert-deftest test-seq-every-p ()
(with-test-sequences (seq '(43 54 22 1))
(should (seq-every-p (lambda (elt) t) seq))
@@ -436,5 +444,18 @@ test-sequences-oddp
(should (equal (seq-rest lst) '(2 3)))
(should (equal (seq-rest vec) [2 3]))))
+;; Regression tests for bug#34852
+(progn
+ (ert-deftest test-seq-intersection-with-nil ()
+ (should (equal (seq-intersection '(1 2 nil) '(1 nil)) '(1 nil))))
+
+ (ert-deftest test-seq-set-equal-p-with-nil ()
+ (should (seq-set-equal-p '("a" "b" nil)
+ '(nil "b" "a"))))
+
+ (ert-deftest test-difference-with-nil ()
+ (should (equal (seq-difference '(1 nil) '(2 nil))
+ '(1)))))
+
(provide 'seq-tests)
;;; seq-tests.el ends here
--
2.20.1
[-- Attachment #1.3: Type: text/plain, Size: 6 bytes --]
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]
next prev parent reply other threads:[~2019-03-20 20:51 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-14 2:16 bug#34852: 26.1; seq-intersection ignores nil as element Miguel V. S. Frasson
2019-03-14 12:07 ` Michael Heerdegen
2019-03-14 13:09 ` Nicolas Petton
2019-03-14 12:22 ` Basil L. Contovounesios
2019-03-14 12:52 ` Miguel V. S. Frasson
2019-03-14 16:16 ` Basil L. Contovounesios
2019-03-14 13:09 ` Michael Heerdegen
2019-03-14 13:34 ` Stefan Monnier
2019-03-14 16:19 ` Basil L. Contovounesios
2019-03-14 16:45 ` Michael Heerdegen
2019-03-14 17:14 ` Basil L. Contovounesios
2019-03-14 19:08 ` Miguel V. S. Frasson
2019-03-14 21:43 ` Stefan Monnier
2019-03-14 23:08 ` Miguel V. S. Frasson
2019-03-14 23:14 ` Stefan Monnier
2019-03-14 23:21 ` Miguel V. S. Frasson
2019-03-14 23:42 ` Michael Heerdegen
2019-03-15 2:40 ` Stefan Monnier
2019-03-15 12:26 ` Michael Heerdegen
2019-03-15 14:47 ` Stefan Monnier
2019-03-14 23:45 ` Miguel V. S. Frasson
2019-03-14 23:15 ` Michael Heerdegen
2019-03-15 15:56 ` Basil L. Contovounesios
2019-03-15 16:08 ` Miguel V. S. Frasson
2019-03-16 20:33 ` Miguel V. S. Frasson
2019-03-16 20:49 ` Basil L. Contovounesios
2019-03-16 21:32 ` Miguel V. S. Frasson
2019-03-15 15:55 ` Basil L. Contovounesios
2019-03-14 16:17 ` Basil L. Contovounesios
2019-03-14 16:35 ` Michael Heerdegen
2019-03-14 17:02 ` Basil L. Contovounesios
2019-03-14 17:23 ` Basil L. Contovounesios
2019-03-14 16:45 ` Nicolas Petton
2019-03-14 17:08 ` Basil L. Contovounesios
2019-03-18 11:55 ` Nicolas Petton
2019-03-18 19:06 ` Michael Heerdegen
2019-03-18 20:14 ` Nicolas Petton
2019-03-20 20:51 ` Nicolas Petton [this message]
2019-03-20 22:33 ` Michael Heerdegen
2019-03-21 8:02 ` Nicolas Petton
2019-03-21 17:46 ` Basil L. Contovounesios
2019-03-21 20:01 ` Nicolas Petton
2019-03-21 20:16 ` Nicolas Petton
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87r2b1nub1.fsf@petton.fr \
--to=nicolas@petton.fr \
--cc=34852@debbugs.gnu.org \
--cc=contovob@tcd.ie \
--cc=michael_heerdegen@web.de \
--cc=monnier@iro.umontreal.ca \
--cc=mvsfrasson@gmail.com \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.