From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Nicolas Petton Newsgroups: gmane.emacs.bugs Subject: bug#34852: 26.1; seq-intersection ignores nil as element Date: Wed, 20 Mar 2019 21:51:30 +0100 Message-ID: <87r2b1nub1.fsf@petton.fr> References: <87zhpxabn3.fsf@tcd.ie> <87va0lmwlg.fsf@web.de> <87d0mtpfqb.fsf@petton.fr> <87k1h1criw.fsf@tcd.ie> <87h8c0mm74.fsf@petton.fr> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="133068"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Michael Heerdegen , "Miguel V. S. Frasson" , 34852@debbugs.gnu.org, Stefan Monnier To: "Basil L. Contovounesios" Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Mar 20 21:54:39 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h6iEO-000YUq-Lk for geb-bug-gnu-emacs@m.gmane.org; Wed, 20 Mar 2019 21:54:36 +0100 Original-Received: from localhost ([127.0.0.1]:53038 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h6iEN-0001DX-L9 for geb-bug-gnu-emacs@m.gmane.org; Wed, 20 Mar 2019 16:54:35 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:44785) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h6iE8-0001Ch-QX for bug-gnu-emacs@gnu.org; Wed, 20 Mar 2019 16:54:23 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h6iBy-00014p-9n for bug-gnu-emacs@gnu.org; Wed, 20 Mar 2019 16:52:08 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:38689) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h6iBu-0000uK-7h for bug-gnu-emacs@gnu.org; Wed, 20 Mar 2019 16:52:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1h6iBt-0004ic-S8 for bug-gnu-emacs@gnu.org; Wed, 20 Mar 2019 16:52:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Nicolas Petton Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 20 Mar 2019 20:52:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 34852 X-GNU-PR-Package: emacs Original-Received: via spool by 34852-submit@debbugs.gnu.org id=B34852.155311510618117 (code B ref 34852); Wed, 20 Mar 2019 20:52:01 +0000 Original-Received: (at 34852) by debbugs.gnu.org; 20 Mar 2019 20:51:46 +0000 Original-Received: from localhost ([127.0.0.1]:52233 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1h6iBe-0004i9-7X for submit@debbugs.gnu.org; Wed, 20 Mar 2019 16:51:46 -0400 Original-Received: from petton.fr ([89.234.186.68]:58048) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1h6iBb-0004ht-Hk for 34852@debbugs.gnu.org; Wed, 20 Mar 2019 16:51:44 -0400 In-Reply-To: <87h8c0mm74.fsf@petton.fr> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=petton.fr; s=mail; t=1553115095; bh=Niupp7HBbtt4NXGLLaArGVUa5NshH6Z3A2ytJSATAIM=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID:MIME-Version:Content-Type; b=OiCm138fpTywz6g357vcLTaOxy63tayvyro/S5Sb/MMVRgJwVj5gc6y/nrIvptXxYU06vR2V0KsZrvbbnWQlpgseq91C50FNnb4vV7dFyuqgtf/h9mFnAjMko8DKei4uoMhC1OTIy+uwiZLlWIkDvCmM4phjFJm7Jbo+FttdSzk= X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:156549 Archived-At: --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Hi, Here's a patch for master. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-New-seq-contains-p-predicate-Bug-34852.patch Content-Transfer-Encoding: quoted-printable From=20985aa028f05a418dddfd6394b739f42c32b01345 Mon Sep 17 00:00:00 2001 From: Nicolas Petton 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. =2D-- 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 =2D-- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi @@ -782,10 +782,11 @@ Sequence Functions @end defun =20 =20 =2D@defun seq-contains sequence elt &optional function =2D This function returns the first element in @var{sequence} that is equa= l to =2D@var{elt}. If the optional argument @var{function} is non-@code{nil}, =2Dit is a function of two arguments to use instead of the default @code{eq= ual}. +@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}. =20 @example @group diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 4a811d7895..39c93e25ed 100644 =2D-- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -356,6 +356,7 @@ seq-sort-by count)) =20 (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)) =20 +(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, re= gardless of order. Equality is defined by TESTFN if non-nil or by `equal' if nil." =2D (and (seq-every-p (lambda (item1) (seq-contains sequence2 item1 testfn= )) sequence1) =2D (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))) =20 (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) =2D (unless (seq-contains result elt testfn) + (unless (seq-contains-p result elt testfn) (setq result (cons elt result)))) (nreverse result))) =20 @@ -410,7 +420,7 @@ seq-sort-by "Return a list of the elements that appear in both SEQUENCE1 and SEQUENC= E2. Equality is defined by TESTFN if non-nil or by `equal' if nil." (seq-reduce (lambda (acc elt) =2D (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 SEQUE= NCE2. Equality is defined by TESTFN if non-nil or by `equal' if nil." (seq-reduce (lambda (acc elt) =2D (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-t= ests.el index d8f00cfea4..6522def423 100644 =2D-- 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 (=3D 5 (seq-contains seq 5))))) =20 +(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])))) =20 +;; 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 =2D-=20 2.20.1 --=-=-= Content-Type: text/plain Nico --=-=-=-- --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEE1AWqLIYsVPF+7mvg6LzXhmr8+XgFAlySp9IACgkQ6LzXhmr8 +XgvZwgAxPAWBfJLufyv0eeoqyM5eJ8TTdVeCkmMxAqXBKHpYZWeJtsF7bUbmCWF bXVP4kgQL+M8gtYxNOhqi3/qjENoTD2gn9p3nyJBVoHxrBly4WZhQRH9Fc1cagaQ GDsyq5LOavAOW9fGMSbnvm2MNr+oJ6SqkuhJsPGLhNBuYEnH1gEsy1x7i6OQQ9cb zPw62FYUiaYh0tr1k1ke4W5bBWoEY+N7fZm3wCfC7jplO2dzJO18Oyf3CBlR6T5t enkgOzVbNvznEmEy3ocdz8HQ7aOqYfrp8TUytOuqJyKYVKDVJskctD++EWLumeHh bc6DFTsG6DkQceKt12LvFMQH22FwLw== =WBIm -----END PGP SIGNATURE----- --==-=-=--