[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master 04d604e: New function seq-position
From: |
Nicolas Petton |
Subject: |
[Emacs-diffs] master 04d604e: New function seq-position |
Date: |
Mon, 19 Oct 2015 22:39:35 +0000 |
branch: master
commit 04d604e0553f76ea9ab266cf44c1d3f89f8bd2f0
Author: Nicolas Petton <address@hidden>
Commit: Nicolas Petton <address@hidden>
New function seq-position
* lisp/emacs-lisp/seq.el (seq-position): New function.
* test/automated/seq-tests.el: New tests for seq-position.
* doc/lispref/sequences.texi: Add documentation for `seq-position'.
---
doc/lispref/sequences.texi | 19 +++++++++++++++++++
lisp/emacs-lisp/seq.el | 15 +++++++++++++--
test/automated/seq-tests.el | 9 +++++++++
3 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 0a6f4c6..8ecae7b 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -743,6 +743,25 @@ it is a function of two arguments to use instead of the
default @code{equal}.
@end defun
address@hidden seq-position sequence elt &optional function
+ This function returns the index of the first element in
address@hidden that is equal to @var{elt}. If the optional argument
address@hidden is address@hidden, it is a function of two arguments to
+use instead of the default @code{equal}.
+
address@hidden
address@hidden
+(seq-position '(a b c) 'b)
address@hidden 1
address@hidden group
address@hidden
+(seq-position '(a b c) 'd)
address@hidden nil
address@hidden group
address@hidden example
address@hidden defun
+
+
@defun seq-uniq sequence &optional function
This function returns a list of the elements of @var{sequence} with
duplicates removed. If the optional argument @var{function} is address@hidden,
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index ce6645a..f5189c7 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -4,7 +4,7 @@
;; Author: Nicolas Petton <address@hidden>
;; Keywords: sequences
-;; Version: 2.0
+;; Version: 2.1
;; Package: seq
;; Maintainer: address@hidden
@@ -294,12 +294,23 @@ found or not."
count))
(cl-defgeneric seq-contains (seq elt &optional testfn)
- "Return the first element in SEQ that equals to ELT.
+ "Return the first element in SEQ that is equal 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))
+(cl-defgeneric seq-position (seq elt &optional testfn)
+ "Return the index of the first element in SEQ that is equal to ELT.
+Equality is defined by TESTFN if non-nil or by `equal' if nil."
+ (let ((index 0))
+ (catch 'seq--break
+ (seq-doseq (e seq)
+ (when (funcall (or testfn #'equal) e elt)
+ (throw 'seq--break index))
+ (setq index (1+ index)))
+ nil)))
+
(cl-defgeneric seq-uniq (seq &optional testfn)
"Return a list of the elements of SEQ with duplicates removed.
TESTFN is used to compare elements, or `equal' if TESTFN is nil."
diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el
index 7023c94..5d93682 100644
--- a/test/automated/seq-tests.el
+++ b/test/automated/seq-tests.el
@@ -328,5 +328,14 @@ Evaluate BODY for each created sequence.
(should (eq seq (seq-into-sequence seq)))
(should-error (seq-into-sequence 2))))
+(ert-deftest test-seq-position ()
+ (with-test-sequences (seq '(2 4 6))
+ (should (null (seq-position seq 1)))
+ (should (= (seq-position seq 4) 1)))
+ (let ((seq '(a b c)))
+ (should (null (seq-position seq 'd #'eq)))
+ (should (= (seq-position seq 'a #'eq) 0))
+ (should (null (seq-position seq (make-symbol "a") #'eq)))))
+
(provide 'seq-tests)
;;; seq-tests.el ends here
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] master 04d604e: New function seq-position,
Nicolas Petton <=