From: Nicolas Petton Date: Mon, 19 Oct 2015 22:11:30 +0000 (+0200) Subject: New function seq-position X-Git-Tag: emacs-25.0.90~1085 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=04d604e0553f76ea9ab266cf44c1d3f89f8bd2f0;p=emacs.git 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'. --- diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 0a6f4c6623c..8ecae7b58b5 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 +@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}, diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index ce6645a099a..f5189c7dc97 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -4,7 +4,7 @@ ;; Author: Nicolas Petton ;; 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." diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el index 7023c94c0c7..5d936828fbb 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