;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: sequences
-;; Version: 2.16
+;; Version: 2.17
;; Package: seq
;; Maintainer: emacs-devel@gnu.org
(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))
'(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)))