all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: Nicolas Petton <nicolas@petton.fr>
Cc: Stefan Monnier <monnier@iro.umontreal.ca>,
	Emacs Development <emacs-devel@gnu.org>
Subject: Re: Stream implementation of seq-mapn
Date: Tue, 26 Dec 2017 23:09:04 +0100	[thread overview]
Message-ID: <87wp19qgzj.fsf@web.de> (raw)
In-Reply-To: <87o9mulsi3.fsf@petton.fr> (Nicolas Petton's message of "Tue, 19 Dec 2017 15:08:52 +0100")

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

Hello,

I uploaded a slightly tweaked version and added regression tests (but
didn't bump the package version).  I also reported the method signature
problem as "bug#29786: 27.0.50; About the argument list of methods".
Unanswered until now, so the generated help for the method still looks
weird.

For completeness, here is what I committed:


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

From 93e9009c827bc5b0f67c683a4e1748c90c6e4686 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' method for streams

* stream/stream.el (seq-mapn): New method.
* stream/tests/stream-tests.el (stream-seq-mapn-test): New test.
And add a `deftest-for-delayed-evaluation'.
---
 packages/stream/stream.el             | 22 ++++++++++++++++++++++
 packages/stream/tests/stream-tests.el | 15 +++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/packages/stream/stream.el b/packages/stream/stream.el
index b412807e2..3f6bc4b5b 100644
--- a/packages/stream/stream.el
+++ b/packages/stream/stream.el
@@ -321,6 +321,28 @@ 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 (seq-every-p #'streamp streams))
+      (cl-call-next-method)
+    (cl-labels ((do-mapn (f streams)
+                         (stream-make
+                          (unless (seq-some #'stream-empty-p streams)
+                            (cons (apply f (mapcar #'stream-first streams))
+                                  (do-mapn f (mapcar #'stream-rest streams)))))))
+      (do-mapn function (cons stream streams)))))
+
 (cl-defmethod seq-do (function (stream stream))
   "Evaluate FUNCTION for each element of STREAM eagerly, and return nil.
 
diff --git a/packages/stream/tests/stream-tests.el b/packages/stream/tests/stream-tests.el
index decf3ad47..896c72993 100644
--- a/packages/stream/tests/stream-tests.el
+++ b/packages/stream/tests/stream-tests.el
@@ -166,6 +166,18 @@
   (should (= -1 (stream-first (seq-map #'- (stream-range 1)))))
   (should (= -2 (stream-first (stream-rest (seq-map #'- (stream-range 1)))))))
 
+(ert-deftest stream-seq-mapn-test ()
+  (should (streamp (seq-mapn #'+ (stream (list 1 2 3)) (stream (list 4 5 6)))))
+  (should (not (streamp (seq-mapn #'+ (stream (list 1 2 3)) (stream (list 4 5 6)) (list 7 8 9)))))
+  (should (= 2 (seq-length (seq-mapn #'+ (stream (list 1 2 3)) (stream (list 4 5))))))
+  (should (equal (letrec ((fibs (stream-cons
+                                 1
+                                 (stream-cons
+                                  1
+                                  (seq-mapn #'+ fibs (stream-rest fibs))))))
+                   (seq-into (seq-take fibs 10) 'list))
+                 '(1 1 2 3 5 8 13 21 34 55))))
+
 (ert-deftest stream-seq-do-test ()
   (let ((result '()))
     (seq-do
@@ -292,6 +304,9 @@
 (deftest-for-delayed-evaluation (seq-drop (make-delayed-test-stream) 2))
 (deftest-for-delayed-evaluation (seq-take-while #'numberp (make-delayed-test-stream)))
 (deftest-for-delayed-evaluation (seq-map #'identity (make-delayed-test-stream)))
+(deftest-for-delayed-evaluation (seq-mapn #'cons
+                                          (make-delayed-test-stream)
+                                          (make-delayed-test-stream)))
 (deftest-for-delayed-evaluation (seq-filter #'cl-evenp (make-delayed-test-stream)))
 (deftest-for-delayed-evaluation (stream-delay (make-delayed-test-stream)))
 (deftest-for-delayed-evaluation (seq-copy (make-delayed-test-stream)))
-- 
2.15.1


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



Thanks,

Michael.

  reply	other threads:[~2017-12-26 22:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2017-12-29  9:04             ` Nicolas Petton
2017-12-30 14:28               ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wp19qgzj.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=nicolas@petton.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.