]> git.eshelyaron.com Git - emacs.git/commitdiff
New function seq-position
authorNicolas Petton <nicolas@petton.fr>
Mon, 19 Oct 2015 22:11:30 +0000 (00:11 +0200)
committerNicolas Petton <nicolas@petton.fr>
Mon, 19 Oct 2015 22:39:27 +0000 (00:39 +0200)
* 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
lisp/emacs-lisp/seq.el
test/automated/seq-tests.el

index 0a6f4c6623c19bd5104cf9d6a8646f27eb0db6af..8ecae7b58b5a04dd01d8d3b0419925d18ac26cef 100644 (file)
@@ -743,6 +743,25 @@ it is a function of two arguments to use instead of the default @code{equal}.
 
 @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
+@var{function} is non-@code{nil}, it is a function of two arguments to
+use instead of the default @code{equal}.
+
+@example
+@group
+(seq-position '(a b c) 'b)
+@result{} 1
+@end group
+@group
+(seq-position '(a b c) 'd)
+@result{} nil
+@end group
+@end example
+@end 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 non-@code{nil},
index ce6645a099a927183b91238267701a8a168aae08..f5189c7dc97518474558cb42be8e24f70cc588f5 100644 (file)
@@ -4,7 +4,7 @@
 
 ;; Author: Nicolas Petton <nicolas@petton.fr>
 ;; Keywords: sequences
-;; Version: 2.0
+;; Version: 2.1
 ;; Package: seq
 
 ;; Maintainer: emacs-devel@gnu.org
@@ -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."
index 7023c94c0c76b706362c534e06c2c06c9e886283..5d936828fbb275f4f9aab5d2c40f8f41e3527fdf 100644 (file)
@@ -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