[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/stream bd614d022b 2/7: Add more efficient method for ma
From: |
Stefan Monnier |
Subject: |
[elpa] externals/stream bd614d022b 2/7: Add more efficient method for making streams from arrays. |
Date: |
Sun, 17 Nov 2024 21:15:42 -0500 (EST) |
branch: externals/stream
commit bd614d022b043397e7294a5083e4dcf3f805e089
Author: Earl Hyatt <okamsn@protonmail.com>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
Add more efficient method for making streams from arrays.
* stream.el (stream): Add method for arrays that avoids creating
sub-sequences.
* tests/stream-tests.el (stream-array-test): Add test for new method.
---
stream.el | 18 ++++++++++++++++++
tests/stream-tests.el | 4 ++++
2 files changed, 22 insertions(+)
diff --git a/stream.el b/stream.el
index 2439f958cb..300f3b8ae2 100644
--- a/stream.el
+++ b/stream.el
@@ -173,6 +173,24 @@ SEQ can be a list, vector or string."
(seq-elt seq 0)
(stream (seq-subseq seq 1)))))
+(cl-defmethod stream ((array array))
+ "Return a stream built from the array ARRAY."
+ (let ((len (length array)))
+ (if (= len 0)
+ (stream-empty)
+ ;; This approach could avoid one level of indirection by setting
+ ;; `stream--updater' directly, but using `funcall' makes for a
+ ;; good example of how to use a custom updater function using the public
+ ;; interface.
+ (let ((idx 0))
+ (cl-labels ((updater ()
+ (if (< idx len)
+ (prog1 (cons (aref array idx)
+ (stream-make (funcall #'updater)))
+ (setq idx (1+ idx)))
+ nil)))
+ (stream-make (funcall #'updater)))))))
+
(cl-defmethod stream ((list list))
"Return a stream built from the list LIST."
(if (null list)
diff --git a/tests/stream-tests.el b/tests/stream-tests.el
index ba304f1ee4..997dd8607b 100644
--- a/tests/stream-tests.el
+++ b/tests/stream-tests.el
@@ -234,6 +234,10 @@
(dolist (list '(nil '(1 2 3) '(a . b)))
(should (equal list (seq-into (stream list) 'list)))))
+(ert-deftest stream-array-test ()
+ (dolist (arr (list "cat" [0 1 2]))
+ (should (equal arr (seq-into (stream arr) (type-of arr))))))
+
(ert-deftest stream-seq-subseq-test ()
(should (stream-empty-p (seq-subseq (stream-range 2 10) 0 0)))
(should (= (stream-first (seq-subseq (stream-range 2 10) 0 3)) 2))
- [elpa] externals/stream updated (5c0ffd088f -> 1e3c1436cc), Stefan Monnier, 2024/11/17
- [elpa] externals/stream b2f650a8a6 4/7: Add an implementation of `seq-concatenate` for streams., Stefan Monnier, 2024/11/17
- [elpa] externals/stream 0ad03f585b 5/7: Add test for delayed evaluation for seq-drop-while for streams., Stefan Monnier, 2024/11/17
- [elpa] externals/stream daa19594e7 3/7: Add generalized variables for streams that error when used., Stefan Monnier, 2024/11/17
- [elpa] externals/stream 1e3c1436cc 7/7: Shorten the documentation strings of seq-take-while and seq-drop-while., Stefan Monnier, 2024/11/17
- [elpa] externals/stream bd614d022b 2/7: Add more efficient method for making streams from arrays.,
Stefan Monnier <=
- [elpa] externals/stream f48552c066 1/7: Change 'stream.el' to use structures instead of cons cells., Stefan Monnier, 2024/11/17
- [elpa] externals/stream 202db1e4b5 6/7: Fix the quoting in the documentation of `seq-mapn` for streams., Stefan Monnier, 2024/11/17