New function seq-map-indexed

* lisp/emacs-lisp/seq.el (seq-map-indexed): New function.
* test/lisp/emacs-lisp/seq-tests.el: Add tests for seq-map-indexed.
This commit is contained in:
Nicolas Petton 2016-02-14 10:25:10 +01:00
parent d9bf0c1c6a
commit 30fe90fa3c
2 changed files with 22 additions and 0 deletions

View file

@ -144,6 +144,18 @@ if positive or too small if negative)."
sequence)
(nreverse result)))
(defun seq-map-indexed (function sequence)
"Return the result of applying FUNCTION to each element of SEQUENCE.
Unlike `seq-map', FUNCTION takes two arguments: the element of
the sequence, and its index within the sequence."
(let ((index 0))
(seq-map (lambda (elt)
(prog1
(funcall function elt index)
(setq index (1+ index))))
sequence)))
;; faster implementation for sequences (sequencep)
(cl-defmethod seq-map (function (sequence sequence))
(mapcar function sequence))