* 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'.
@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},
;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: sequences
-;; Version: 2.0
+;; Version: 2.1
;; Package: seq
;; Maintainer: emacs-devel@gnu.org
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."
(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