emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Stream implementation of seq-mapn


From: Michael Heerdegen
Subject: Stream implementation of seq-mapn
Date: Mon, 18 Dec 2017 12:58:16 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Hello,

I want to define `seq-mapn' for streams.  This is what I have:


>From 46caedf7b85f4285e83f72646fa1d6940bd7ea00 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <address@hidden>
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


Questions:

(1) Is it ok to implement it with `cl-call-next-method' this way?

(2) When I do C-h f seq-mapn, the signature of this new method is
printed like

| (arg2 (arg3 stream) &rest rest)

Why is that?


Thanks,

Michael.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]