From 7dfe247864f12b93b906edb5934af3c356acade4 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 28 Oct 2015 14:27:39 +0000 Subject: [PATCH] * lisp/emacs-lisp/seq.el (seq-mapn): New function * doc/lispref/sequences.texi (Sequence Functions): Document seq-mapn --- doc/lispref/sequences.texi | 18 ++++++++++++++++++ lisp/emacs-lisp/seq.el | 17 ++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 8ecae7b58b5..730dac10452 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi @@ -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} diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index d0c2d24b015..68265094c17 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -4,7 +4,7 @@ ;; Author: Nicolas Petton ;; 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. -- 2.39.5