emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master 88f96e6: Add seq-set-equal-p to test for set equali


From: Nicolas Petton
Subject: [Emacs-diffs] master 88f96e6: Add seq-set-equal-p to test for set equality
Date: Thu, 4 May 2017 05:33:26 -0400 (EDT)

branch: master
commit 88f96e69cfcd265f2ef0db3e134ac9e29e64ec3e
Author: Damien Cassou <address@hidden>
Commit: Nicolas Petton <address@hidden>

    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 @@ it is a function of two arguments to use instead of the 
default @code{equal}.
 
 @end defun
 
address@hidden 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 address@hidden, it is a function of two
+arguments to use instead of the default @code{equal}.
+
address@hidden
address@hidden
+(seq-set-equal-p '(a b c) '(c b a))
address@hidden t
address@hidden group
address@hidden
+(seq-set-equal-p '(a b c) '(c b))
address@hidden nil
address@hidden group
address@hidden
+(seq-set-equal-p '("a" "b" "c") '("c" "b" "a"))
address@hidden t
address@hidden group
address@hidden
+(seq-set-equal-p '("a" "b" "c") '("c" "b" "a") #'eq)
address@hidden nil
address@hidden group
address@hidden example
+
address@hidden 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 d79eecf..73c088c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -899,6 +899,9 @@ instead of its first.
 
 * 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 @@ Equality is defined by TESTFN if non-nil or by `equal' if 
nil."
                 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 @@ Evaluate BODY for each created sequence.
     (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)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]