From: Damien Cassou <damien@cassou.me>
To: Drew Adams <drew.adams@oracle.com>
Cc: 26540@debbugs.gnu.org
Subject: bug#26540: 25.2; [PATCH] Add cl-set-equal to test for set equality
Date: Tue, 18 Apr 2017 16:40:19 +0200 [thread overview]
Message-ID: <87wpahbwz0.fsf@cassou.me> (raw)
In-Reply-To: <30ea6b31-dc72-4402-9e06-823c2174b826@default>
[-- Attachment #1: Type: text/plain, Size: 1113 bytes --]
Drew Adams <drew.adams@oracle.com> writes:
> I'm just pointing out that a function we already have, and one
> that is used more widely by users of Common Lisp, does the same
> thing - unless I'm missing something.
I agree (except that one has opposite result).
> If people think that some users might not think to use
> `set-exclusive-or' to test set equality then we could add a
> `set-equal' function. Common Lisp didn't think so, and neither
> do I, but I wouldn't oppose adding it.
At least I didn't think about using exclusive-or. Searching for
"equal" or "same elements" in the info page (info "(cl) Lists as
Sets") didn't help.
> If we do add it, I'd imagine that the implementation should be
> the same (adding `not', as you say), for clarity and consistency
> - unless other things are not equal for some reason (i.e.,
> unless there is a good reason not to use the existing
> implementation).
I updated the patch.
--
Damien Cassou
http://damiencassou.seasidehosting.st
"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-cl-set-equal-to-test-for-set-equality.patch --]
[-- Type: text/x-patch, Size: 3795 bytes --]
From f3f46edeb47178ebe6dbdcbe72bf150788167dcf Mon Sep 17 00:00:00 2001
From: Damien Cassou <damien@cassou.me>
Date: Mon, 17 Apr 2017 11:01:39 +0200
Subject: [PATCH] Add cl-set-equal to test for set equality
* lisp/emacs-lisp/cl-seq.el (cl-set-equal): Add function to compare
two lists as if they were sets.
* test/lisp/emacs-lisp/cl-seq-tests.el (cl-set-equal): Add test for
cl-set-equal.
---
doc/misc/cl.texi | 6 ++++++
etc/NEWS | 3 +++
lisp/emacs-lisp/cl-seq.el | 9 +++++++++
test/lisp/emacs-lisp/cl-seq-tests.el | 16 ++++++++++++++++
4 files changed, 34 insertions(+)
diff --git a/doc/misc/cl.texi b/doc/misc/cl.texi
index 2339d57..aa64ae2 100644
--- a/doc/misc/cl.texi
+++ b/doc/misc/cl.texi
@@ -3917,6 +3917,12 @@ Lists as Sets
also appears in @var{list2}.
@end defun
+@defun cl-set-equal list1 list2 @t{&key :test :key}
+This function checks whether every element of @var{list1} also appears
+in @var{list2} and if every element of @var{list2} also appears in
+@var{list1}.
+@end defun
+
@node Association Lists
@section Association Lists
diff --git a/etc/NEWS b/etc/NEWS
index 76c9dbc..e22a440 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -874,6 +874,9 @@ instead of its first.
\f
* Lisp Changes in Emacs 26.1
+** New function 'cl-set-equal' to check if every element of LIST1 also
+appears in LIST2 and if every element of LIST2 also appears in LIST1.
+
+++
** Emacs now supports records for user-defined types, via the new
functions 'make-record', 'record', and 'recordp'. Records are now
diff --git a/lisp/emacs-lisp/cl-seq.el b/lisp/emacs-lisp/cl-seq.el
index 67ff1a0..9467d41 100644
--- a/lisp/emacs-lisp/cl-seq.el
+++ b/lisp/emacs-lisp/cl-seq.el
@@ -923,6 +923,15 @@ cl-subsetp
(null cl-list1)))))
;;;###autoload
+(defun cl-set-equal (cl-list1 cl-list2 &rest cl-keys)
+ "Return true if LIST1 and LIST2 have same elements.
+I.e., if every element of LIST1 also appears in LIST2 and if
+every element of LIST2 also appears in LIST1.
+\nKeywords supported: :test :key \n(fn LIST1 LIST2
+[KEYWORD VALUE]...)"
+ (not (apply 'cl-set-exclusive-or cl-list1 cl-list2 cl-keys)))
+
+;;;###autoload
(defun cl-subst-if (cl-new cl-pred cl-tree &rest cl-keys)
"Substitute NEW for elements matching PREDICATE in TREE (non-destructively).
Return a copy of TREE with all matching elements replaced by NEW.
diff --git a/test/lisp/emacs-lisp/cl-seq-tests.el b/test/lisp/emacs-lisp/cl-seq-tests.el
index 61e3d72..0347ca4 100644
--- a/test/lisp/emacs-lisp/cl-seq-tests.el
+++ b/test/lisp/emacs-lisp/cl-seq-tests.el
@@ -292,6 +292,22 @@ cl-seq--with-side-effects
(should (= 1 (cl-search (nthcdr 2 list) (nthcdr 2 list2))))
(should (= 3 (cl-search (nthcdr 2 list) list2)))))
+;; keywords supported: :test :key
+(ert-deftest cl-set-equal ()
+ (should (cl-set-equal '(1 2 3) '(1 2 3)))
+ (should (cl-set-equal '(1 2 3) '(3 2 1)))
+ (should (cl-set-equal '(3 2 1) '(1 2 3)))
+ (should-not (cl-set-equal '(2 3) '(3 2 1)))
+ (should-not (cl-set-equal '(1 2 3) '(2 3)))
+ (should-not (cl-set-equal '("1" "2") '("2" "1") :test #'eq))
+ (should (cl-set-equal '("1" "2") '("2" "1") :test #'equal))
+ (should-not (cl-set-equal '(1 2) '(-1 -2)))
+ (should (cl-set-equal '(1 2) '(-1 -2) :key #'abs))
+ (should-not (cl-set-equal '(("1" 1) ("2" 1)) '(("1" 2) ("2" 2))))
+ (should-not (cl-set-equal '(("1" 1) ("2" 1)) '(("1" 2) ("2" 2)) :key #'car))
+ (should-not (cl-set-equal '(("1" 1) ("2" 1)) '(("1" 2) ("2" 2)) :test #'equal))
+ (should (cl-set-equal '(("1" 1) ("2" 1)) '(("1" 2) ("2" 2)) :key #'car :test #'equal)))
+
(ert-deftest cl-seq-test-bug24264 ()
"Test for http://debbugs.gnu.org/24264 ."
(let ((list (append (make-list 8000005 1) '(8)))
--
2.9.3
next prev parent reply other threads:[~2017-04-18 14:40 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-17 9:16 bug#26540: 25.2; [PATCH] Add cl-set-equal to test for set equality Damien Cassou
2017-04-17 13:55 ` Drew Adams
2017-04-18 11:21 ` Damien Cassou
2017-04-18 14:00 ` Drew Adams
2017-04-18 14:40 ` Damien Cassou [this message]
2017-04-18 21:49 ` Drew Adams
2017-04-18 20:13 ` John Mastro
2017-04-18 21:53 ` Drew Adams
2017-04-19 9:39 ` Nicolas Petton
2017-04-19 10:43 ` Damien Cassou
2017-04-19 11:39 ` Damien Cassou
2017-04-19 14:41 ` Nicolas Petton
2017-05-03 13:02 ` Damien Cassou
2017-05-04 9:41 ` Nicolas Petton
2017-04-19 21:19 ` Michael Heerdegen
2017-05-03 13:12 ` Damien Cassou
2017-05-11 19:42 ` Michael Heerdegen
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=87wpahbwz0.fsf@cassou.me \
--to=damien@cassou.me \
--cc=26540@debbugs.gnu.org \
--cc=drew.adams@oracle.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.