[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/stream a36469b 17/36: Add some more basic stream operat
From: |
Stefan Monnier |
Subject: |
[elpa] externals/stream a36469b 17/36: Add some more basic stream operations |
Date: |
Tue, 1 Dec 2020 17:22:50 -0500 (EST) |
branch: externals/stream
commit a36469bfad76a8a0b0ab9c0fa032c3143c33dc78
Author: Michael Heerdegen <michael_heerdegen@web.de>
Commit: Michael Heerdegen <michael_heerdegen@web.de>
Add some more basic stream operations
---
stream.el | 45 +++++++++++++++++++++++++++++++++++++++++++++
tests/stream-tests.el | 23 +++++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git a/stream.el b/stream.el
index 22cecac..62eb3b6 100644
--- a/stream.el
+++ b/stream.el
@@ -333,6 +333,51 @@ calling this function."
(cl-defmethod seq-copy ((stream stream))
"Return a shallow copy of STREAM."
(stream-delay stream))
+
+
+;;; More stream operations
+
+(defun stream-scan (function init stream)
+ "Return a stream of successive reduced values for STREAM.
+
+If the elements of a stream s are s_1, s_2, ..., the elements
+S_1, S_2, ... of the stream returned by \(stream-scan f init s\)
+are defined recursively by
+
+ S_1 = init
+ S_(n+1) = (funcall f S_n s_n)
+
+as long as s_n exists.
+
+Example:
+
+ (stream-scan #'* 1 (stream-range 1))
+
+returns a stream of the factorials."
+ (let ((res init))
+ (stream-cons
+ res
+ (seq-map (lambda (el) (setq res (funcall function res el)))
+ stream))))
+
+(defun stream-flush (stream)
+ "Request all elements from STREAM in order for side effects only."
+ (while (not (stream-empty-p stream))
+ (cl-callf stream-rest stream)))
+
+(defun stream-iterate-function (function value)
+ "Return a stream of repeated applications of FUNCTION to VALUE.
+The returned stream starts with VALUE. Any successive element
+will be found by calling FUNCTION on the preceding element."
+ (stream-cons
+ value
+ (stream-iterate-function function (funcall function value))))
+
+(defun stream-concatenate (stream-of-streams)
+ "Concatenate all streams in STREAM-OF-STREAMS and return the result.
+All elements in STREAM-OF-STREAMS must be streams. The result is
+a stream."
+ (seq-reduce #'stream-append stream-of-streams (stream-empty)))
(defun stream-of-directory-files-1 (directory &optional nosort recurse
follow-links)
"Helper for `stream-of-directory-files'."
diff --git a/tests/stream-tests.el b/tests/stream-tests.el
index 23a54b5..16b5756 100644
--- a/tests/stream-tests.el
+++ b/tests/stream-tests.el
@@ -242,5 +242,28 @@
(should (= 2 (stream-first str)))
(should (null (stream-pop stream-empty)))))
+(ert-deftest stream-scan-test ()
+ (should (eq (seq-elt (stream-scan #'* 1 (stream-range 1)) 4) 24)))
+
+(ert-deftest stream-flush-test ()
+ (should (let* ((times 0)
+ (count (lambda () (cl-incf times))))
+ (letrec ((make-test-stream (lambda () (stream-cons (progn (funcall
count) nil)
+ (funcall
make-test-stream)))))
+ (stream-flush (seq-take (funcall make-test-stream) 5))
+ (eq times 5)))))
+
+(ert-deftest stream-iterate-function-test ()
+ (should (equal (list 0 1 2) (seq-into-sequence (seq-take
(stream-iterate-function #'1+ 0) 3)))))
+
+(ert-deftest stream-concatenate-test ()
+ (should (equal (seq-into-sequence
+ (stream-concatenate
+ (stream (list (stream (list 1 2 3))
+ (stream (list))
+ (stream (list 4))
+ (stream (list 5 6 7 8 9))))))
+ (list 1 2 3 4 5 6 7 8 9))))
+
(provide 'stream-tests)
;;; stream-tests.el ends here
- [elpa] externals/stream f384660 22/36: Add systematic tests against bogus element generation, (continued)
- [elpa] externals/stream f384660 22/36: Add systematic tests against bogus element generation, Stefan Monnier, 2020/12/01
- [elpa] externals/stream ec86243 24/36: Some details in the documentation, Stefan Monnier, 2020/12/01
- [elpa] externals/stream b0c8949 28/36: Implement `seq-mapn' method for streams, Stefan Monnier, 2020/12/01
- [elpa] externals/stream 7874a25 05/36: * packages/stream/tests/stream-tests.el: New tests., Stefan Monnier, 2020/12/01
- [elpa] externals/stream d77b733 09/36: Update stream.el to v2.0.2., Stefan Monnier, 2020/12/01
- [elpa] externals/stream fbe7ebd 06/36: Update stream.el to version 2.0.0, Stefan Monnier, 2020/12/01
- [elpa] externals/stream a8aa25c 07/36: * packages/stream/stream.el: Update stream.el to version 2.0.1., Stefan Monnier, 2020/12/01
- [elpa] externals/stream 2b2ba28 14/36: update stream.el to the latest version, Stefan Monnier, 2020/12/01
- [elpa] externals/stream 00e523f 15/36: Add stream stream-delay and stream-of-directory-files, Stefan Monnier, 2020/12/01
- [elpa] externals/stream ac42cc1 18/36: Fix some quoting problems in doc strings, Stefan Monnier, 2020/12/01
- [elpa] externals/stream a36469b 17/36: Add some more basic stream operations,
Stefan Monnier <=
- [elpa] externals/stream f8e6639 20/36: * stream/stream.el (stream--generalizer): Accept more arguments, Stefan Monnier, 2020/12/01
- [elpa] externals/stream 873ee4e 23/36: Fix errors detected by tests added in last commit, Stefan Monnier, 2020/12/01
- [elpa] externals/stream cbf7e78 30/36: * stream/stream-x.el: Update copyright, Stefan Monnier, 2020/12/01
- [elpa] externals/stream f1fce5e 35/36: * packages/stream/stream.el: Bump version to 2.2.5., Stefan Monnier, 2020/12/01
- [elpa] externals/stream 05b20e1 25/36: Add file "stream-x.el" to the stream package, Stefan Monnier, 2020/12/01
- [elpa] externals/stream 76ed94e 27/36: Fix seq-empty-p for streams, Stefan Monnier, 2020/12/01
- [elpa] externals/stream 89f61bf 33/36: * packages/stream/stream.el (stream--force): Small fix (Bug#37888)., Stefan Monnier, 2020/12/01
- [elpa] externals/stream d531a58 19/36: Avoid recursive stream-append in stream-concatenate, Stefan Monnier, 2020/12/01
- [elpa] externals/stream 40e5fb1 26/36: Remove an outdated test, Stefan Monnier, 2020/12/01
- [elpa] externals/stream 9c7aef0 34/36: * [stream] Update copyright years, Stefan Monnier, 2020/12/01