branch: master commit 3047633b6738364a504e86e0c0e1110968ae1bc7 Author: Nicolas Petton <nico...@petton.fr> Commit: Nicolas Petton <nico...@petton.fr>
Update seq.el to version 1.9 * packages/seq/seq.el: Update to version 1.9. * packages/seq/tests/seq-tests.el: Update to version 1.9. --- packages/seq/seq.el | 33 ++++++++++++++++++++++++--------- packages/seq/tests/seq-tests.el | 31 +++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/packages/seq/seq.el b/packages/seq/seq.el index 93fefd0..3fd7cf7 100644 --- a/packages/seq/seq.el +++ b/packages/seq/seq.el @@ -4,7 +4,7 @@ ;; Author: Nicolas Petton <nico...@petton.fr> ;; Keywords: sequences -;; Version: 1.8 +;; Version: 1.9 ;; Package: seq ;; Maintainer: emacs-de...@gnu.org @@ -164,13 +164,28 @@ If SEQ is empty, return INITIAL-VALUE and FUNCTION is not called." (setq acc (funcall function acc elt))) acc))) -(defun seq-some-p (pred seq) - "Return any element for which (PRED element) is non-nil in SEQ, nil otherwise." +(defun seq-some (pred seq) + "Return non-nil if (PRED element) is non-nil for any element in SEQ, nil otherwise. +If so, return the non-nil value returned by PRED." + (catch 'seq--break + (seq-doseq (elt seq) + (let ((result (funcall pred elt))) + (when result + (throw 'seq--break result)))) + nil)) + +(defun seq-find (pred seq &optional default) + "Return the first element for which (PRED element) is non-nil in SEQ. +If no element is found, return DEFAULT. + +Note that `seq-find' has an ambiguity if the found element is +identical to DEFAULT, as it cannot be known if an element was +found or not." (catch 'seq--break (seq-doseq (elt seq) (when (funcall pred elt) (throw 'seq--break elt))) - nil)) + default)) (defun seq-every-p (pred seq) "Return non-nil if (PRED element) is non-nil for all elements of the sequence SEQ." @@ -202,10 +217,10 @@ The result is a sequence of the same type as SEQ." (let ((result (seq-sort pred (append seq nil)))) (seq-into result (type-of seq))))) -(defun seq-contains-p (seq elt &optional testfn) +(defun seq-contains (seq elt &optional testfn) "Return the first element in SEQ that equals to ELT. Equality is defined by TESTFN if non-nil or by `equal' if nil." - (seq-some-p (lambda (e) + (seq-some (lambda (e) (funcall (or testfn #'equal) elt e)) seq)) @@ -214,7 +229,7 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." TESTFN is used to compare elements, or `equal' if TESTFN is nil." (let ((result '())) (seq-doseq (elt seq) - (unless (seq-contains-p result elt testfn) + (unless (seq-contains result elt testfn) (setq result (cons elt result)))) (nreverse result))) @@ -272,7 +287,7 @@ negative integer or 0, nil is returned." "Return a list of the elements that appear in both SEQ1 and SEQ2. Equality is defined by TESTFN if non-nil or by `equal' if nil." (seq-reduce (lambda (acc elt) - (if (seq-contains-p seq2 elt testfn) + (if (seq-contains seq2 elt testfn) (cons elt acc) acc)) (seq-reverse seq1) @@ -282,7 +297,7 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." "Return a list of the elements that appear in SEQ1 but not in SEQ2. Equality is defined by TESTFN if non-nil or by `equal' if nil." (seq-reduce (lambda (acc elt) - (if (not (seq-contains-p seq2 elt testfn)) + (if (not (seq-contains seq2 elt testfn)) (cons elt acc) acc)) (seq-reverse seq1) diff --git a/packages/seq/tests/seq-tests.el b/packages/seq/tests/seq-tests.el index 3643ce5..deedfb8 100644 --- a/packages/seq/tests/seq-tests.el +++ b/packages/seq/tests/seq-tests.el @@ -124,21 +124,32 @@ Evaluate BODY for each created sequence. (should (eq (seq-reduce #'+ seq 0) 0)) (should (eq (seq-reduce #'+ seq 7) 7)))) -(ert-deftest test-seq-some-p () +(ert-deftest test-seq-some () (with-test-sequences (seq '(4 3 2 1)) - (should (= (seq-some-p #'test-sequences-evenp seq) 4)) - (should (= (seq-some-p #'test-sequences-oddp seq) 3)) - (should-not (seq-some-p (lambda (elt) (> elt 10)) seq))) + (should (seq-some #'test-sequences-evenp seq)) + (should (seq-some #'test-sequences-oddp seq)) + (should-not (seq-some (lambda (elt) (> elt 10)) seq))) (with-test-sequences (seq '()) - (should-not (seq-some-p #'test-sequences-oddp seq)))) + (should-not (seq-some #'test-sequences-oddp seq))) + (should (seq-some #'null '(1 nil 2)))) -(ert-deftest test-seq-contains-p () +(ert-deftest test-seq-find () + (with-test-sequences (seq '(4 3 2 1)) + (should (= 4 (seq-find #'test-sequences-evenp seq))) + (should (= 3 (seq-find #'test-sequences-oddp seq))) + (should-not (seq-find (lambda (elt) (> elt 10)) seq))) + (should-not (seq-find #'null '(1 nil 2))) + (should-not (seq-find #'null '(1 nil 2) t)) + (should-not (seq-find #'null '(1 2 3))) + (should (seq-find #'null '(1 2 3) 'sentinel))) + +(ert-deftest test-seq-contains () (with-test-sequences (seq '(3 4 5 6)) - (should (seq-contains-p seq 3)) - (should-not (seq-contains-p seq 7))) + (should (seq-contains seq 3)) + (should-not (seq-contains seq 7))) (with-test-sequences (seq '()) - (should-not (seq-contains-p seq 3)) - (should-not (seq-contains-p seq nil)))) + (should-not (seq-contains seq 3)) + (should-not (seq-contains seq nil)))) (ert-deftest test-seq-every-p () (with-test-sequences (seq '(43 54 22 1))