* 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
* Re: Stream implementation of seq-mapn
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
0 siblings, 1 reply; 9+ messages in thread
From: Nicolas Petton @ 2017-12-18 12:32 UTC (permalink / raw)
To: Michael Heerdegen, Emacs Development; +Cc: Stefan Monnier
[-- Attachment #1: Type: text/plain, Size: 1152 bytes --]
Michael Heerdegen <michael_heerdegen@web.de> writes:
Hi Michael,
> [...]
> +(cl-defmethod seq-mapn (function (stream stream) &rest streams)
> + "Map FUNCTION over the STREAMS.
I would add to the docstring that all elements of `streams' should be
streams, otherwise it will default to the generic implementation.
> +
> +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))
^^^^^^^^
Since this is for stream.el, why not use `seq-every-p'?
> + (cl-call-next-method)
> + (cl-callf2 cons stream streams)
> + (stream-make
> + (unless (cl-some #'seq-empty-p streams)
^^^^^^^
Same question :-)
> [...]
> (1) Is it ok to implement it with `cl-call-next-method' this way?
I guess it is. Have you tried running `seq-mapn' with a mix of streams
and lists?
Cheers,
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stream implementation of seq-mapn
2017-12-18 12:32 ` Nicolas Petton
@ 2017-12-18 12:51 ` Michael Heerdegen
2017-12-18 13:50 ` Nicolas Petton
0 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2017-12-18 12:51 UTC (permalink / raw)
To: Nicolas Petton; +Cc: Stefan Monnier, Emacs Development
Nicolas Petton <nicolas@petton.fr> writes:
> > [...]
> > +(cl-defmethod seq-mapn (function (stream stream) &rest streams)
> > + "Map FUNCTION over the STREAMS.
> I would add to the docstring that all elements of `streams' should be
> streams, otherwise it will default to the generic implementation.
I already had "all the STREAMS must be streams", but that looked weird.
> > + (if (not (cl-every #'streamp streams))
> ^^^^^^^^
> Since this is for stream.el, why not use `seq-every-p'?
> > + (cl-call-next-method)
> > + (cl-callf2 cons stream streams)
> > + (stream-make
> > + (unless (cl-some #'seq-empty-p streams)
> ^^^^^^^
> Same question :-)
Ok, done.
> > (1) Is it ok to implement it with `cl-call-next-method' this way?
>
> I guess it is. Have you tried running `seq-mapn' with a mix of streams
> and lists?
Sure. I does what I want: the default method is invoked.
Michael.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stream implementation of seq-mapn
2017-12-18 12:51 ` Michael Heerdegen
@ 2017-12-18 13:50 ` Nicolas Petton
2017-12-19 13:05 ` Michael Heerdegen
0 siblings, 1 reply; 9+ messages in thread
From: Nicolas Petton @ 2017-12-18 13:50 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: Stefan Monnier, Emacs Development
[-- Attachment #1: Type: text/plain, Size: 134 bytes --]
Michael Heerdegen <michael_heerdegen@web.de> writes:
> Sure. I does what I want: the default method is invoked.
That's fine.
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stream implementation of seq-mapn
2017-12-18 13:50 ` Nicolas Petton
@ 2017-12-19 13:05 ` Michael Heerdegen
2017-12-19 14:08 ` Nicolas Petton
0 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2017-12-19 13:05 UTC (permalink / raw)
To: Nicolas Petton; +Cc: Stefan Monnier, Emacs Development
Nicolas Petton <nicolas@petton.fr> writes:
> That's fine.
Great.
While preparing the commit I noticed these two problems:
(1) Why does (seq-empty-p (stream-empty)) return nil? That's at least a
pitfall, or a bug. Why is there no implementation of `seq-empty-p' in
stream.el (I know stream-empty-p exists)?
(2) In stream-tests.el, there is now an undefined function
`seq-take-until'.
Thanks,
Michael.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stream implementation of seq-mapn
2017-12-19 13:05 ` Michael Heerdegen
@ 2017-12-19 14:08 ` Nicolas Petton
2017-12-26 22:09 ` Michael Heerdegen
0 siblings, 1 reply; 9+ messages in thread
From: Nicolas Petton @ 2017-12-19 14:08 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: Stefan Monnier, Emacs Development
[-- Attachment #1: Type: text/plain, Size: 477 bytes --]
Michael Heerdegen <michael_heerdegen@web.de> writes:
> While preparing the commit I noticed these two problems:
>
> (1) Why does (seq-empty-p (stream-empty)) return nil? That's at least a
> pitfall, or a bug. Why is there no implementation of `seq-empty-p' in
> stream.el (I know stream-empty-p exists)?
That's a bug, I'll post a fix :-)
> (2) In stream-tests.el, there is now an undefined function
> `seq-take-until'.
Ok, I'll post a fix for this as well.
Thanks!
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stream implementation of seq-mapn
2017-12-19 14:08 ` Nicolas Petton
@ 2017-12-26 22:09 ` Michael Heerdegen
2017-12-29 9:04 ` Nicolas Petton
0 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2017-12-26 22:09 UTC (permalink / raw)
To: Nicolas Petton; +Cc: Stefan Monnier, Emacs Development
[-- 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.
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: Stream implementation of seq-mapn
2017-12-26 22:09 ` Michael Heerdegen
@ 2017-12-29 9:04 ` Nicolas Petton
2017-12-30 14:28 ` Michael Heerdegen
0 siblings, 1 reply; 9+ messages in thread
From: Nicolas Petton @ 2017-12-29 9:04 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: Stefan Monnier, Emacs Development
[-- Attachment #1: Type: text/plain, Size: 570 bytes --]
Michael Heerdegen <michael_heerdegen@web.de> writes:
> Hello,
Hi Michael,
> 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:
Your additions have improved thunk.el/stream.el quite a lot, thank
you!
I'd be curious to see your use cases for these libraries.
Cheers,
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stream implementation of seq-mapn
2017-12-29 9:04 ` Nicolas Petton
@ 2017-12-30 14:28 ` Michael Heerdegen
0 siblings, 0 replies; 9+ messages in thread
From: Michael Heerdegen @ 2017-12-30 14:28 UTC (permalink / raw)
To: Nicolas Petton; +Cc: Stefan Monnier, Emacs Development
Nicolas Petton <nicolas@petton.fr> writes:
> I'd be curious to see your use cases for these libraries.
Currently, I mainly use them in el-search. I also have used streams to
implement "helm-browse' which is something like "swiper" but uses
streams in the search engine to make it fast and extensible. It's quite
cool, but I haven't published it so far, mainly because I don't use Helm
that much in the meantime, and swiper itself is quite nice.
I use streams here and there in my init files instead of lists where it
makes sense, e.g. for traversing directory hierarchies.
So, not too much cool stuff...
Michael.
^ permalink raw reply [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 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.