aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Petton2015-10-20 00:11:30 +0200
committerNicolas Petton2015-10-20 00:39:27 +0200
commit04d604e0553f76ea9ab266cf44c1d3f89f8bd2f0 (patch)
tree2f5b54b355421705fd5dfec28bd9f88d16165e26
parentb911b4b25db93c8b574a5dc6f1258893b4aa18c4 (diff)
downloademacs-04d604e0553f76ea9ab266cf44c1d3f89f8bd2f0.tar.gz
emacs-04d604e0553f76ea9ab266cf44c1d3f89f8bd2f0.zip
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'.
-rw-r--r--doc/lispref/sequences.texi19
-rw-r--r--lisp/emacs-lisp/seq.el15
-rw-r--r--test/automated/seq-tests.el9
3 files changed, 41 insertions, 2 deletions
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}.
743 743
744@end defun 744@end defun
745 745
746@defun seq-position sequence elt &optional function
747 This function returns the index of the first element in
748@var{sequence} that is equal to @var{elt}. If the optional argument
749@var{function} is non-@code{nil}, it is a function of two arguments to
750use instead of the default @code{equal}.
751
752@example
753@group
754(seq-position '(a b c) 'b)
755@result{} 1
756@end group
757@group
758(seq-position '(a b c) 'd)
759@result{} nil
760@end group
761@end example
762@end defun
763
764
746@defun seq-uniq sequence &optional function 765@defun seq-uniq sequence &optional function
747 This function returns a list of the elements of @var{sequence} with 766 This function returns a list of the elements of @var{sequence} with
748duplicates removed. If the optional argument @var{function} is non-@code{nil}, 767duplicates 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 @@
4 4
5;; Author: Nicolas Petton <nicolas@petton.fr> 5;; Author: Nicolas Petton <nicolas@petton.fr>
6;; Keywords: sequences 6;; Keywords: sequences
7;; Version: 2.0 7;; Version: 2.1
8;; Package: seq 8;; Package: seq
9 9
10;; Maintainer: emacs-devel@gnu.org 10;; Maintainer: emacs-devel@gnu.org
@@ -294,12 +294,23 @@ found or not."
294 count)) 294 count))
295 295
296(cl-defgeneric seq-contains (seq elt &optional testfn) 296(cl-defgeneric seq-contains (seq elt &optional testfn)
297 "Return the first element in SEQ that equals to ELT. 297 "Return the first element in SEQ that is equal to ELT.
298Equality is defined by TESTFN if non-nil or by `equal' if nil." 298Equality is defined by TESTFN if non-nil or by `equal' if nil."
299 (seq-some (lambda (e) 299 (seq-some (lambda (e)
300 (funcall (or testfn #'equal) elt e)) 300 (funcall (or testfn #'equal) elt e))
301 seq)) 301 seq))
302 302
303(cl-defgeneric seq-position (seq elt &optional testfn)
304 "Return the index of the first element in SEQ that is equal to ELT.
305Equality is defined by TESTFN if non-nil or by `equal' if nil."
306 (let ((index 0))
307 (catch 'seq--break
308 (seq-doseq (e seq)
309 (when (funcall (or testfn #'equal) e elt)
310 (throw 'seq--break index))
311 (setq index (1+ index)))
312 nil)))
313
303(cl-defgeneric seq-uniq (seq &optional testfn) 314(cl-defgeneric seq-uniq (seq &optional testfn)
304 "Return a list of the elements of SEQ with duplicates removed. 315 "Return a list of the elements of SEQ with duplicates removed.
305TESTFN is used to compare elements, or `equal' if TESTFN is nil." 316TESTFN 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.
328 (should (eq seq (seq-into-sequence seq))) 328 (should (eq seq (seq-into-sequence seq)))
329 (should-error (seq-into-sequence 2)))) 329 (should-error (seq-into-sequence 2))))
330 330
331(ert-deftest test-seq-position ()
332 (with-test-sequences (seq '(2 4 6))
333 (should (null (seq-position seq 1)))
334 (should (= (seq-position seq 4) 1)))
335 (let ((seq '(a b c)))
336 (should (null (seq-position seq 'd #'eq)))
337 (should (= (seq-position seq 'a #'eq) 0))
338 (should (null (seq-position seq (make-symbol "a") #'eq)))))
339
331(provide 'seq-tests) 340(provide 'seq-tests)
332;;; seq-tests.el ends here 341;;; seq-tests.el ends here