]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/seq.el
Add seq-do-indexed
[gnu-emacs] / lisp / emacs-lisp / seq.el
index 92f0ad78566c78cfa46f38cd77b8c09f6bdb6ae3..20610a7027d13caab4b2e47f35da61d784754be9 100644 (file)
@@ -4,7 +4,7 @@
 
 ;; Author: Nicolas Petton <nicolas@petton.fr>
 ;; Keywords: sequences
-;; Version: 2.14
+;; 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))
@@ -471,10 +481,7 @@ If no element is found, return nil."
 
 (cl-defmethod seq-drop ((list list) n)
   "Optimized implementation of `seq-drop' for lists."
-  (while (and list (> n 0))
-    (setq list (cdr list)
-          n (1- n)))
-  list)
+  (nthcdr n list))
 
 (cl-defmethod seq-take ((list list) n)
   "Optimized implementation of `seq-take' for lists."