[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master aeb1d6b 2/4: Improve the semantic of seq-some
From: |
Nicolas Petton |
Subject: |
[Emacs-diffs] master aeb1d6b 2/4: Improve the semantic of seq-some |
Date: |
Sat, 05 Sep 2015 22:56:05 +0000 |
branch: master
commit aeb1d6bdd54671a2e2b7dfbd22fcfe1aa19b36d1
Author: Nicolas Petton <address@hidden>
Commit: Nicolas Petton <address@hidden>
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.
---
doc/lispref/sequences.texi | 11 ++++++++---
lisp/emacs-lisp/seq.el | 12 +++++++-----
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 22ae995..f73779b 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -558,18 +558,23 @@ calling @var{function}.
@end defun
@defun seq-some predicate sequence
- This function returns the first member of sequence for which @var{predicate}
-returns address@hidden
+ This function returns address@hidden if @var{predicate} returns
address@hidden for any element of @var{sequence}. If so, the returned
+value is the value returned by @var{predicate}.
@example
@group
(seq-some #'numberp ["abc" 1 nil])
address@hidden 1
address@hidden t
@end group
@group
(seq-some #'numberp ["abc" "def"])
@result{} nil
@end group
address@hidden
+(seq-some #'null ["abc" 1 nil])
address@hidden t
address@hidden group
@end example
@end defun
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index bf5495b..8dc9147 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."
t))
(cl-defgeneric seq-some (pred seq)
- "Return any element for which (PRED element) is non-nil in SEQ, nil
otherwise."
+ "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)
- (when (funcall pred elt)
- (throw 'seq--break elt)))
+ (let ((result (funcall pred elt)))
+ (when result
+ (throw 'seq--break result))))
nil))
(cl-defgeneric seq-count (pred seq)
@@ -280,8 +282,8 @@ If SEQ is empty, return INITIAL-VALUE and FUNCTION is not
called."
"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 (lambda (e)
- (funcall (or testfn #'equal) elt e))
- seq))
+ (funcall (or testfn #'equal) elt e))
+ seq))
(cl-defgeneric seq-uniq (seq &optional testfn)
"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 efbb90d..07a183d 100644
--- a/test/automated/seq-tests.el
+++ b/test/automated/seq-tests.el
@@ -131,11 +131,12 @@ Evaluate BODY for each created sequence.
(ert-deftest test-seq-some ()
(with-test-sequences (seq '(4 3 2 1))
- (should (= (seq-some #'test-sequences-evenp seq) 4))
- (should (= (seq-some #'test-sequences-oddp seq) 3))
+ (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 #'test-sequences-oddp seq))))
+ (should-not (seq-some #'test-sequences-oddp seq)))
+ (should (seq-some #'null '(1 nil 2))))
(ert-deftest test-seq-contains ()
(with-test-sequences (seq '(3 4 5 6))