mirror of
https://git.savannah.gnu.org/git/emacs.git
synced 2024-12-20 10:23:57 +00:00
Add seq-do-indexed
* lisp/emacs-lisp/seq.el (seq-do-indexed): New function. * test/lisp/emacs-lisp/seq-tests.el: Add a unit test for seq-do-indexed.
This commit is contained in:
parent
44eca25a4b
commit
9726856f29
@ -4,7 +4,7 @@
|
||||
|
||||
;; Author: Nicolas Petton <nicolas@petton.fr>
|
||||
;; Keywords: sequences
|
||||
;; Version: 2.16
|
||||
;; Version: 2.17
|
||||
;; Package: seq
|
||||
|
||||
;; Maintainer: emacs-devel@gnu.org
|
||||
@ -117,6 +117,16 @@ Return SEQUENCE."
|
||||
|
||||
(defalias 'seq-each #'seq-do)
|
||||
|
||||
(defun seq-do-indexed (function sequence)
|
||||
"Apply FUNCTION to each element of SEQUENCE and return nil.
|
||||
Unlike `seq-map', FUNCTION takes two arguments: the element of
|
||||
the sequence, and its index within the sequence."
|
||||
(let ((index 0))
|
||||
(seq-do (lambda (elt)
|
||||
(funcall function elt index)
|
||||
(setq index (1+ index)))
|
||||
sequence)))
|
||||
|
||||
(cl-defgeneric seqp (sequence)
|
||||
"Return non-nil if SEQUENCE is a sequence, nil otherwise."
|
||||
(sequencep sequence))
|
||||
|
@ -107,6 +107,21 @@ Evaluate BODY for each created sequence.
|
||||
'(a b c d))
|
||||
'((a 0) (b 1) (c 2) (d 3)))))
|
||||
|
||||
(ert-deftest test-seq-do-indexed ()
|
||||
(let ((result nil))
|
||||
(seq-do-indexed (lambda (elt i)
|
||||
(add-to-list 'result (list elt i)))
|
||||
nil)
|
||||
(should (equal result nil)))
|
||||
(with-test-sequences (seq '(4 5 6))
|
||||
(let ((result nil))
|
||||
(seq-do-indexed (lambda (elt i)
|
||||
(add-to-list 'result (list elt i)))
|
||||
seq)
|
||||
(should (equal (seq-elt result 0) '(6 2)))
|
||||
(should (equal (seq-elt result 1) '(5 1)))
|
||||
(should (equal (seq-elt result 2) '(4 0))))))
|
||||
|
||||
(ert-deftest test-seq-filter ()
|
||||
(with-test-sequences (seq '(6 7 8 9 10))
|
||||
(should (equal (seq-filter #'test-sequences-evenp seq) '(6 8 10)))
|
||||
|
Loading…
Reference in New Issue
Block a user