all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Damien Cassou <damien@cassou.me>
To: Nicolas Petton <nicolas@petton.fr>,
	John Mastro <john.b.mastro@gmail.com>,
	26540@debbugs.gnu.org
Subject: bug#26540: 25.2; [PATCH] Add cl-set-equal to test for set equality
Date: Wed, 03 May 2017 15:02:28 +0200	[thread overview]
Message-ID: <87fugmt7ob.fsf@cassou.me> (raw)
In-Reply-To: <87r30oh33m.fsf@petton.fr>

[-- Attachment #1: Type: text/plain, Size: 468 bytes --]

Nicolas Petton <nicolas@petton.fr> writes:
> Damien Cassou <damien@cassou.me> writes:
>> +(cl-defgeneric seq-set-equal (sequence1 sequence2 &optional 
>> testfn)
>                   ^^^^^^^^^^^^^
>                   What about `seq-set-equal-p'?
> [...]


I applied your changes and attach a new 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-seq-set-equal-p-to-test-for-set-equality.patch --]
[-- Type: text/x-patch, Size: 4568 bytes --]

From f63a0b5aff1a065a17169dfbd622cc7e91648195 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 seq-set-equal-p to test for set equality

* lisp/emacs-lisp/seq.el (seq-set-equal-p): Add function to compare
  two lists as if they were sets.

* test/lisp/emacs-lisp/seq-tests.el (test-seq-set-equal-p): Add test
  for seq-set-equal-p.
---
 doc/lispref/sequences.texi        | 27 +++++++++++++++++++++++++++
 etc/NEWS                          |  3 +++
 lisp/emacs-lisp/seq.el            |  6 ++++++
 test/lisp/emacs-lisp/seq-tests.el | 25 +++++++++++++++++++++++++
 4 files changed, 61 insertions(+)

diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 93e8fa8..c7cf9f5 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -792,6 +792,33 @@ Sequence Functions
 
 @end defun
 
+@defun seq-set-equal-p sequence1 sequence2 &optional testfn
+This function checks whether @var{sequence1} and @var{sequence2}
+contain the same elements, regardless of the order. If the optional
+argument @var{testfn} is non-@code{nil}, it is a function of two
+arguments to use instead of the default @code{equal}.
+
+@example
+@group
+(seq-set-equal-p '(a b c) '(c b a))
+@result{} t
+@end group
+@group
+(seq-set-equal-p '(a b c) '(c b))
+@result{} nil
+@end group
+@group
+(seq-set-equal-p '("a" "b" "c") '("c" "b" "a"))
+@result{} t
+@end group
+@group
+(seq-set-equal-p '("a" "b" "c") '("c" "b" "a") #'eq)
+@result{} nil
+@end group
+@end example
+
+@end defun
+
 @defun seq-position sequence elt &optional function
   This function returns the index of the first element in
 @var{sequence} that is equal to @var{elt}.  If the optional argument
diff --git a/etc/NEWS b/etc/NEWS
index 410e681..6d0ee9b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -894,6 +894,9 @@ instead of its first.
 \f
 * Lisp Changes in Emacs 26.1
 
+** New function 'seq-set-equal-p' to check if SEQUENCE1 and SEQUENCE2
+contain the same elements, regardless of the order.
+
 +++
 ** 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/seq.el b/lisp/emacs-lisp/seq.el
index 10de248..963a1dd 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -355,6 +355,12 @@ (cl-defgeneric seq-contains (sequence elt &optional testfn)
                 e))
             sequence))
 
+(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)))
+
 (cl-defgeneric seq-position (sequence elt &optional testfn)
   "Return the index of the first element in SEQUENCE that is equal to ELT.
 Equality is defined by TESTFN if non-nil or by `equal' if nil."
diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el
index 788524b..495cf1e 100644
--- a/test/lisp/emacs-lisp/seq-tests.el
+++ b/test/lisp/emacs-lisp/seq-tests.el
@@ -197,6 +197,31 @@ (ert-deftest test-seq-every-p ()
     (should (seq-every-p #'identity seq))
     (should (seq-every-p #'test-sequences-evenp seq))))
 
+(ert-deftest test-seq-set-equal-p ()
+  (with-test-sequences (seq1 '(1 2 3))
+    (should (seq-set-equal-p seq1 seq1))
+    (should (seq-set-equal-p seq1 seq1 #'eq))
+
+    (with-test-sequences (seq2 '(3 2 1))
+      (should (seq-set-equal-p seq1 seq2))
+      (should (seq-set-equal-p seq2 seq1))
+      (should (seq-set-equal-p seq1 seq2 #'eq))
+      (should (seq-set-equal-p seq2 seq1 #'eq)))
+
+    (with-test-sequences (seq2 '(3 1))
+      (should-not (seq-set-equal-p seq1 seq2))
+      (should-not (seq-set-equal-p seq2 seq1))))
+
+  (should (seq-set-equal-p '("a" "b" "c")
+                           '("c" "b" "a")))
+  (should-not (seq-set-equal-p '("a" "b" "c")
+                               '("c" "b" "a") #'eq))
+  (should-not (seq-set-equal-p '(("a" 1) ("b" 1) ("c" 1))
+                               '(("c" 2) ("b" 2) ("a" 2))))
+  (should (seq-set-equal-p '(("a" 1) ("b" 1) ("c" 1))
+                           '(("c" 2) ("b" 2) ("a" 2))
+                           (lambda (i1 i2) (equal (car i1) (car i2))))))
+
 (ert-deftest test-seq-empty-p ()
   (with-test-sequences (seq '(0))
     (should-not (seq-empty-p seq)))
-- 
2.9.3


  reply	other threads:[~2017-05-03 13:02 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
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 [this message]
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=87fugmt7ob.fsf@cassou.me \
    --to=damien@cassou.me \
    --cc=26540@debbugs.gnu.org \
    --cc=john.b.mastro@gmail.com \
    --cc=nicolas@petton.fr \
    /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.