]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/emacs-lisp/seq.el (seq-mapn): New function
authorArtur Malabarba <bruce.connor.am@gmail.com>
Wed, 28 Oct 2015 14:27:39 +0000 (14:27 +0000)
committerArtur Malabarba <bruce.connor.am@gmail.com>
Wed, 28 Oct 2015 15:40:23 +0000 (15:40 +0000)
* doc/lispref/sequences.texi (Sequence Functions): Document seq-mapn

doc/lispref/sequences.texi
lisp/emacs-lisp/seq.el

index 8ecae7b58b5a04dd01d8d3b0419925d18ac26cef..730dac10452098dc859698f201a948ac0151e416 100644 (file)
@@ -572,6 +572,24 @@ element of @var{sequence}.  The returned value is a list.
 @end example
 @end defun
 
+@defun seq-mapn function &rest sequences
+  This function returns the result of applying @var{function} to each
+element of @var{sequences}.  The arity of @var{function} must match
+the number of sequences.  Mapping stops at the shrotest sequence, and
+the returned value is a list.
+
+@example
+@group
+(seq-mapn #'+ '(2 4 6) '(20 40 60))
+@result{} (22 44 66)
+@end group
+@group
+(seq-mapn #'concat '("moskito" "bite") ["bee" "sting"])
+@result{} ("moskitobee" "bitesting")
+@end group
+@end example
+@end defun
+
 @defun seq-filter predicate sequence
 @cindex filtering sequences
   This function returns a list of all the elements in @var{sequence}
index d0c2d24b015e446f424bb646ee2230480d6e99bf..68265094c17dddbbffcec24a50f36e7b5f6b8cf8 100644 (file)
@@ -4,7 +4,7 @@
 
 ;; Author: Nicolas Petton <nicolas@petton.fr>
 ;; Keywords: sequences
-;; Version: 2.1
+;; Version: 2.2
 ;; Package: seq
 
 ;; Maintainer: emacs-devel@gnu.org
@@ -148,6 +148,21 @@ if positive or too small if negative)."
 (cl-defmethod seq-map (function (sequence sequence))
   (mapcar function sequence))
 
+(cl-defgeneric seq-mapn (function sequence &rest sequences)
+  "Like `seq-map' but FUNCTION is mapped over all SEQUENCES.
+The arity of FUNCTION must match the number of SEQUENCES, and the
+mapping stops on the shortest sequence.
+Return a list of the results.
+
+\(fn FUNCTION SEQUENCES...)"
+  (let ((result nil)
+        (sequences (seq-map (lambda (s) (seq-into s 'list))
+                            (cons sequence sequences))))
+    (while (not (memq nil sequences))
+      (push (apply function (seq-map #'car sequences)) result)
+      (setq sequences (seq-map #'cdr sequences)))
+    (nreverse result)))
+
 (cl-defgeneric seq-drop (sequence n)
   "Remove the first N elements of SEQUENCE and return the result.
 The result is a sequence of the same type as SEQUENCE.