unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Stream implementation of seq-mapn
@ 2017-12-18 11:58 Michael Heerdegen
  2017-12-18 12:32 ` Nicolas Petton
  0 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2017-12-18 11:58 UTC (permalink / raw)
  To: Emacs Development; +Cc: Nicolas Petton, Stefan Monnier

[-- Attachment #1: Type: text/plain, Size: 73 bytes --]

Hello,

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



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Implement-seq-mapn-for-streams.patch --]
[-- Type: text/x-diff, Size: 1520 bytes --]

From 46caedf7b85f4285e83f72646fa1d6940bd7ea00 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen@web.de>
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


[-- Attachment #3: Type: text/plain, Size: 229 bytes --]



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.

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-12-30 14:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-18 11:58 Stream implementation of seq-mapn Michael Heerdegen
2017-12-18 12:32 ` Nicolas Petton
2017-12-18 12:51   ` Michael Heerdegen
2017-12-18 13:50     ` Nicolas Petton
2017-12-19 13:05       ` Michael Heerdegen
2017-12-19 14:08         ` Nicolas Petton
2017-12-26 22:09           ` Michael Heerdegen
2017-12-29  9:04             ` Nicolas Petton
2017-12-30 14:28               ` Michael Heerdegen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).