From b30eaba87be980c8fbaea3c124c3cadd9aec6fe0 Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Mon, 17 Apr 2017 11:01:39 +0200 Subject: [PATCH] Add seq-set-equal to test for set equality * lisp/emacs-lisp/seq.el (seq-set-equal): Add function to compare two lists as if they were sets. * test/lisp/emacs-lisp/seq-tests.el (test-seq-set-equal): Add test for seq-set-equal. --- doc/lispref/sequences.texi | 28 ++++++++++++++++++++++++++++ etc/NEWS | 3 +++ lisp/emacs-lisp/seq.el | 8 ++++++++ test/lisp/emacs-lisp/seq-tests.el | 25 +++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 93e8fa8..2f6fb1d 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi @@ -792,6 +792,34 @@ Sequence Functions @end defun +@defun seq-set-equal sequence1 sequence2 &optional testfn +This function checks whether every element of @var{sequence1} also +appears in @var{sequence2} and if every element of @var{sequence2} +also appears in @var{sequence1}. 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 '(a b c) '(c b a)) +@result{} t +@end group +@group +(seq-set-equal '(a b c) '(c b)) +@result{} nil +@end group +@group +(seq-set-equal '("a" "b" "c") '("c" "b" "a")) +@result{} t +@end group +@group +(seq-set-equal '("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 76c9dbc..9b6c89d 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -874,6 +874,9 @@ instead of its first. * Lisp Changes in Emacs 26.1 +** New function 'seq-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/seq.el b/lisp/emacs-lisp/seq.el index 10de248..40f2988 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -355,6 +355,14 @@ seq-sort-by e)) sequence)) +(cl-defgeneric seq-set-equal (sequence1 sequence2 &optional testfn) + "Return true if SEQUENCE1 and SEQUENCE2 have same elements. +I.e., if every element of SEQUENCE1 also appears in SEQUENCE2 and if +every element of SEQUENCE2 also appears in SEQUENCE1. +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..9cc54d8 100644 --- a/test/lisp/emacs-lisp/seq-tests.el +++ b/test/lisp/emacs-lisp/seq-tests.el @@ -197,6 +197,31 @@ test-sequences-oddp (should (seq-every-p #'identity seq)) (should (seq-every-p #'test-sequences-evenp seq)))) +(ert-deftest test-seq-set-equal () + (with-test-sequences (seq1 '(1 2 3)) + (should (seq-set-equal seq1 seq1)) + (should (seq-set-equal seq1 seq1 #'eq)) + + (with-test-sequences (seq2 '(3 2 1)) + (should (seq-set-equal seq1 seq2)) + (should (seq-set-equal seq2 seq1)) + (should (seq-set-equal seq1 seq2 #'eq)) + (should (seq-set-equal seq2 seq1 #'eq))) + + (with-test-sequences (seq2 '(3 1)) + (should-not (seq-set-equal seq1 seq2)) + (should-not (seq-set-equal seq2 seq1)))) + + (should (seq-set-equal '("a" "b" "c") + '("c" "b" "a"))) + (should-not (seq-set-equal '("a" "b" "c") + '("c" "b" "a") #'eq)) + (should-not (seq-set-equal '(("a" 1) ("b" 1) ("c" 1)) + '(("c" 2) ("b" 2) ("a" 2)))) + (should (seq-set-equal '(("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