diff options
| author | Damien Cassou | 2017-04-17 11:01:39 +0200 |
|---|---|---|
| committer | Nicolas Petton | 2017-05-04 11:32:58 +0200 |
| commit | 88f96e69cfcd265f2ef0db3e134ac9e29e64ec3e (patch) | |
| tree | cdf11ece3a34f982ba3bf28bd8f90d659fdc41b1 | |
| parent | 250d24fa7333046fb187cf4f544dc4358f16e2df (diff) | |
| download | emacs-88f96e69cfcd265f2ef0db3e134ac9e29e64ec3e.tar.gz emacs-88f96e69cfcd265f2ef0db3e134ac9e29e64ec3e.zip | |
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.
| -rw-r--r-- | doc/lispref/sequences.texi | 27 | ||||
| -rw-r--r-- | etc/NEWS | 3 | ||||
| -rw-r--r-- | lisp/emacs-lisp/seq.el | 6 | ||||
| -rw-r--r-- | test/lisp/emacs-lisp/seq-tests.el | 25 |
4 files changed, 61 insertions, 0 deletions
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 93e8fa8a5fa..c7cf9f5e1af 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}. | |||
| 792 | 792 | ||
| 793 | @end defun | 793 | @end defun |
| 794 | 794 | ||
| 795 | @defun seq-set-equal-p sequence1 sequence2 &optional testfn | ||
| 796 | This function checks whether @var{sequence1} and @var{sequence2} | ||
| 797 | contain the same elements, regardless of the order. If the optional | ||
| 798 | argument @var{testfn} is non-@code{nil}, it is a function of two | ||
| 799 | arguments to use instead of the default @code{equal}. | ||
| 800 | |||
| 801 | @example | ||
| 802 | @group | ||
| 803 | (seq-set-equal-p '(a b c) '(c b a)) | ||
| 804 | @result{} t | ||
| 805 | @end group | ||
| 806 | @group | ||
| 807 | (seq-set-equal-p '(a b c) '(c b)) | ||
| 808 | @result{} nil | ||
| 809 | @end group | ||
| 810 | @group | ||
| 811 | (seq-set-equal-p '("a" "b" "c") '("c" "b" "a")) | ||
| 812 | @result{} t | ||
| 813 | @end group | ||
| 814 | @group | ||
| 815 | (seq-set-equal-p '("a" "b" "c") '("c" "b" "a") #'eq) | ||
| 816 | @result{} nil | ||
| 817 | @end group | ||
| 818 | @end example | ||
| 819 | |||
| 820 | @end defun | ||
| 821 | |||
| 795 | @defun seq-position sequence elt &optional function | 822 | @defun seq-position sequence elt &optional function |
| 796 | This function returns the index of the first element in | 823 | This function returns the index of the first element in |
| 797 | @var{sequence} that is equal to @var{elt}. If the optional argument | 824 | @var{sequence} that is equal to @var{elt}. If the optional argument |
| @@ -899,6 +899,9 @@ instead of its first. | |||
| 899 | 899 | ||
| 900 | * Lisp Changes in Emacs 26.1 | 900 | * Lisp Changes in Emacs 26.1 |
| 901 | 901 | ||
| 902 | ** New function 'seq-set-equal-p' to check if SEQUENCE1 and SEQUENCE2 | ||
| 903 | contain the same elements, regardless of the order. | ||
| 904 | |||
| 902 | +++ | 905 | +++ |
| 903 | ** Emacs now supports records for user-defined types, via the new | 906 | ** Emacs now supports records for user-defined types, via the new |
| 904 | functions 'make-record', 'record', and 'recordp'. Records are now | 907 | functions 'make-record', 'record', and 'recordp'. Records are now |
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 10de2484798..963a1ddf964 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." | |||
| 355 | e)) | 355 | e)) |
| 356 | sequence)) | 356 | sequence)) |
| 357 | 357 | ||
| 358 | (cl-defgeneric seq-set-equal-p (sequence1 sequence2 &optional testfn) | ||
| 359 | "Return non-nil if SEQUENCE1 and SEQUENCE2 contain the same elements, regardless of order. | ||
| 360 | Equality is defined by TESTFN if non-nil or by `equal' if nil." | ||
| 361 | (and (seq-every-p (lambda (item1) (seq-contains sequence2 item1 testfn)) sequence1) | ||
| 362 | (seq-every-p (lambda (item2) (seq-contains sequence1 item2 testfn)) sequence2))) | ||
| 363 | |||
| 358 | (cl-defgeneric seq-position (sequence elt &optional testfn) | 364 | (cl-defgeneric seq-position (sequence elt &optional testfn) |
| 359 | "Return the index of the first element in SEQUENCE that is equal to ELT. | 365 | "Return the index of the first element in SEQUENCE that is equal to ELT. |
| 360 | Equality is defined by TESTFN if non-nil or by `equal' if nil." | 366 | 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 788524bedb5..495cf1e543c 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. | |||
| 197 | (should (seq-every-p #'identity seq)) | 197 | (should (seq-every-p #'identity seq)) |
| 198 | (should (seq-every-p #'test-sequences-evenp seq)))) | 198 | (should (seq-every-p #'test-sequences-evenp seq)))) |
| 199 | 199 | ||
| 200 | (ert-deftest test-seq-set-equal-p () | ||
| 201 | (with-test-sequences (seq1 '(1 2 3)) | ||
| 202 | (should (seq-set-equal-p seq1 seq1)) | ||
| 203 | (should (seq-set-equal-p seq1 seq1 #'eq)) | ||
| 204 | |||
| 205 | (with-test-sequences (seq2 '(3 2 1)) | ||
| 206 | (should (seq-set-equal-p seq1 seq2)) | ||
| 207 | (should (seq-set-equal-p seq2 seq1)) | ||
| 208 | (should (seq-set-equal-p seq1 seq2 #'eq)) | ||
| 209 | (should (seq-set-equal-p seq2 seq1 #'eq))) | ||
| 210 | |||
| 211 | (with-test-sequences (seq2 '(3 1)) | ||
| 212 | (should-not (seq-set-equal-p seq1 seq2)) | ||
| 213 | (should-not (seq-set-equal-p seq2 seq1)))) | ||
| 214 | |||
| 215 | (should (seq-set-equal-p '("a" "b" "c") | ||
| 216 | '("c" "b" "a"))) | ||
| 217 | (should-not (seq-set-equal-p '("a" "b" "c") | ||
| 218 | '("c" "b" "a") #'eq)) | ||
| 219 | (should-not (seq-set-equal-p '(("a" 1) ("b" 1) ("c" 1)) | ||
| 220 | '(("c" 2) ("b" 2) ("a" 2)))) | ||
| 221 | (should (seq-set-equal-p '(("a" 1) ("b" 1) ("c" 1)) | ||
| 222 | '(("c" 2) ("b" 2) ("a" 2)) | ||
| 223 | (lambda (i1 i2) (equal (car i1) (car i2)))))) | ||
| 224 | |||
| 200 | (ert-deftest test-seq-empty-p () | 225 | (ert-deftest test-seq-empty-p () |
| 201 | (with-test-sequences (seq '(0)) | 226 | (with-test-sequences (seq '(0)) |
| 202 | (should-not (seq-empty-p seq))) | 227 | (should-not (seq-empty-p seq))) |