From 46caedf7b85f4285e83f72646fa1d6940bd7ea00 Mon Sep 17 00:00:00 2001 From: Michael Heerdegen Date: Mon, 18 Dec 2017 12:49:38 +0100 Subject: [PATCH] Implement `seq-mapn' for streams --- packages/stream/stream.el | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/stream/stream.el b/packages/stream/stream.el index 810adf90d..a0e851015 100644 --- a/packages/stream/stream.el +++ b/packages/stream/stream.el @@ -318,6 +318,27 @@ applications of FUNCTION on each element of STREAM in succession." (cons (funcall function (stream-first stream)) (seq-map function (stream-rest stream)))))) +(cl-defmethod seq-mapn (function (stream stream) &rest streams) + "Map FUNCTION over the STREAMS. + +Example: this prints the first ten Fibonacci numbers: + + (letrec ((fibs (stream-cons + 1 + (stream-cons + 1 + (seq-mapn #'+ fibs (stream-rest fibs)))))) + (seq-do #'print (seq-take fibs 10))) + +\(fn FUNCTION STREAMS...)" + (if (not (cl-every #'streamp streams)) + (cl-call-next-method) + (cl-callf2 cons stream streams) + (stream-make + (unless (cl-some #'seq-empty-p streams) + (cons (apply function (mapcar #'stream-first streams)) + (apply #'seq-mapn function (mapcar #'stream-rest streams))))))) + (cl-defmethod seq-do (function (stream stream)) "Evaluate FUNCTION for each element of STREAM eagerly, and return nil. -- 2.15.1