diff options
| author | Nicolas Petton | 2015-09-06 00:26:17 +0200 |
|---|---|---|
| committer | Nicolas Petton | 2015-09-06 00:44:39 +0200 |
| commit | aeb1d6bdd54671a2e2b7dfbd22fcfe1aa19b36d1 (patch) | |
| tree | 117e8f076fe9edb808781730f09122403855313d | |
| parent | c36663d866e64fcb4b0d94742009d84366e9b54f (diff) | |
| download | emacs-aeb1d6bdd54671a2e2b7dfbd22fcfe1aa19b36d1.tar.gz emacs-aeb1d6bdd54671a2e2b7dfbd22fcfe1aa19b36d1.zip | |
Improve the semantic of seq-some
Update seq-some to return non-nil if the predicate returns non-nil for
any element of the seq, in which case the returned value is the one
returned by the predicate.
* lisp/emacs-lisp/seq.el (seq-some): Update the function and its
docstring.
* test/automated/seq-tests.el (test-seq-some): Add a regression test.
* doc/lispref/sequences.texi (Sequence Functions): Update the
documentation for seq-some.
| -rw-r--r-- | doc/lispref/sequences.texi | 11 | ||||
| -rw-r--r-- | lisp/emacs-lisp/seq.el | 12 | ||||
| -rw-r--r-- | test/automated/seq-tests.el | 7 |
3 files changed, 19 insertions, 11 deletions
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 22ae9959602..f73779bd9ce 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi | |||
| @@ -558,18 +558,23 @@ calling @var{function}. | |||
| 558 | @end defun | 558 | @end defun |
| 559 | 559 | ||
| 560 | @defun seq-some predicate sequence | 560 | @defun seq-some predicate sequence |
| 561 | This function returns the first member of sequence for which @var{predicate} | 561 | This function returns non-@code{nil} if @var{predicate} returns |
| 562 | returns non-@code{nil}. | 562 | non-@code{nil} for any element of @var{sequence}. If so, the returned |
| 563 | value is the value returned by @var{predicate}. | ||
| 563 | 564 | ||
| 564 | @example | 565 | @example |
| 565 | @group | 566 | @group |
| 566 | (seq-some #'numberp ["abc" 1 nil]) | 567 | (seq-some #'numberp ["abc" 1 nil]) |
| 567 | @result{} 1 | 568 | @result{} t |
| 568 | @end group | 569 | @end group |
| 569 | @group | 570 | @group |
| 570 | (seq-some #'numberp ["abc" "def"]) | 571 | (seq-some #'numberp ["abc" "def"]) |
| 571 | @result{} nil | 572 | @result{} nil |
| 572 | @end group | 573 | @end group |
| 574 | @group | ||
| 575 | (seq-some #'null ["abc" 1 nil]) | ||
| 576 | @result{} t | ||
| 577 | @end group | ||
| 573 | @end example | 578 | @end example |
| 574 | @end defun | 579 | @end defun |
| 575 | 580 | ||
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index bf5495baac3..8dc91471312 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el | |||
| @@ -261,11 +261,13 @@ If SEQ is empty, return INITIAL-VALUE and FUNCTION is not called." | |||
| 261 | t)) | 261 | t)) |
| 262 | 262 | ||
| 263 | (cl-defgeneric seq-some (pred seq) | 263 | (cl-defgeneric seq-some (pred seq) |
| 264 | "Return any element for which (PRED element) is non-nil in SEQ, nil otherwise." | 264 | "Return non-nil if (PRED element) is non-nil for any element in SEQ, nil otherwise. |
| 265 | If so, return the non-nil value returned by PRED." | ||
| 265 | (catch 'seq--break | 266 | (catch 'seq--break |
| 266 | (seq-doseq (elt seq) | 267 | (seq-doseq (elt seq) |
| 267 | (when (funcall pred elt) | 268 | (let ((result (funcall pred elt))) |
| 268 | (throw 'seq--break elt))) | 269 | (when result |
| 270 | (throw 'seq--break result)))) | ||
| 269 | nil)) | 271 | nil)) |
| 270 | 272 | ||
| 271 | (cl-defgeneric seq-count (pred seq) | 273 | (cl-defgeneric seq-count (pred seq) |
| @@ -280,8 +282,8 @@ If SEQ is empty, return INITIAL-VALUE and FUNCTION is not called." | |||
| 280 | "Return the first element in SEQ that equals to ELT. | 282 | "Return the first element in SEQ that equals to ELT. |
| 281 | Equality is defined by TESTFN if non-nil or by `equal' if nil." | 283 | Equality is defined by TESTFN if non-nil or by `equal' if nil." |
| 282 | (seq-some (lambda (e) | 284 | (seq-some (lambda (e) |
| 283 | (funcall (or testfn #'equal) elt e)) | 285 | (funcall (or testfn #'equal) elt e)) |
| 284 | seq)) | 286 | seq)) |
| 285 | 287 | ||
| 286 | (cl-defgeneric seq-uniq (seq &optional testfn) | 288 | (cl-defgeneric seq-uniq (seq &optional testfn) |
| 287 | "Return a list of the elements of SEQ with duplicates removed. | 289 | "Return a list of the elements of SEQ with duplicates removed. |
diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el index efbb90dd988..07a183d024e 100644 --- a/test/automated/seq-tests.el +++ b/test/automated/seq-tests.el | |||
| @@ -131,11 +131,12 @@ Evaluate BODY for each created sequence. | |||
| 131 | 131 | ||
| 132 | (ert-deftest test-seq-some () | 132 | (ert-deftest test-seq-some () |
| 133 | (with-test-sequences (seq '(4 3 2 1)) | 133 | (with-test-sequences (seq '(4 3 2 1)) |
| 134 | (should (= (seq-some #'test-sequences-evenp seq) 4)) | 134 | (should (seq-some #'test-sequences-evenp seq)) |
| 135 | (should (= (seq-some #'test-sequences-oddp seq) 3)) | 135 | (should (seq-some #'test-sequences-oddp seq)) |
| 136 | (should-not (seq-some (lambda (elt) (> elt 10)) seq))) | 136 | (should-not (seq-some (lambda (elt) (> elt 10)) seq))) |
| 137 | (with-test-sequences (seq '()) | 137 | (with-test-sequences (seq '()) |
| 138 | (should-not (seq-some #'test-sequences-oddp seq)))) | 138 | (should-not (seq-some #'test-sequences-oddp seq))) |
| 139 | (should (seq-some #'null '(1 nil 2)))) | ||
| 139 | 140 | ||
| 140 | (ert-deftest test-seq-contains () | 141 | (ert-deftest test-seq-contains () |
| 141 | (with-test-sequences (seq '(3 4 5 6)) | 142 | (with-test-sequences (seq '(3 4 5 6)) |