From d5168d49a90ba84114f0578d20e13a725743f65e Mon Sep 17 00:00:00 2001 From: Earl Hyatt Date: Sat, 5 Oct 2024 21:15:24 -0400 Subject: [PATCH 4/5] Add an implementation of `seq-concatenate` for streams. * stream.el (stream): Add a method for creating a stream from a stream, which really just returns the original stream unmodified. * stream.el (seq-concatenate): Add implementation for streams. * tests/stream-tests (stream-seq-concatenate-test, stream-seq-mapcat-test): Add tests for seq-concatenate and seq-mapcat, which by default uses seq-concatenate. --- stream.el | 9 +++++++++ tests/stream-tests.el | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/stream.el b/stream.el index 794b309..18240c8 100644 --- a/stream.el +++ b/stream.el @@ -164,6 +164,10 @@ (defmacro stream-cons (first rest) (cl-defgeneric stream (src) "Return a new stream from SRC.") +(cl-defmethod stream ((stream stream)) + "Return STREAM unmodified." + stream) + (cl-defmethod stream ((seq sequence)) "Return a stream built from the sequence SEQ. SEQ can be a list, vector or string." @@ -492,6 +496,11 @@ (defmacro stream-delay (expr) (cl-defmethod seq-copy ((stream stream)) "Return a shallow copy of STREAM." (stream-delay stream)) + +(cl-defmethod seq-concatenate ((_type (eql stream)) &rest sequences) + "Make a stream which concatenates each sequence in SEQUENCES." + (apply #'stream-append (mapcar #'stream sequences))) + ;;; More stream operations diff --git a/tests/stream-tests.el b/tests/stream-tests.el index 997dd86..5d3f116 100644 --- a/tests/stream-tests.el +++ b/tests/stream-tests.el @@ -212,6 +212,27 @@ (ert-deftest stream-delay-test () (and (equal res1 5) (equal res2 5))))) +(ert-deftest stream-seq-concatenate-test () + (should (streamp (seq-concatenate 'stream (list 1 2) (vector 3 4) (stream (list 5 6))))) + (should (equal '(1 2 3 4 5 6) + (seq-into (seq-concatenate 'stream + (list 1 2) + (vector 3 4) + (stream (list 5 6))) + 'list)))) + +(ert-deftest stream-seq-mapcat-test () + (should (streamp (seq-mapcat #'stream (list (list 1 2) + (vector 3 4) + (stream (list 5 6))) + 'stream))) + (should (equal '(1 2 3 4 5 6) + (seq-into (seq-mapcat #'stream (list (list 1 2) + (vector 3 4) + (stream (list 5 6))) + 'stream) + 'list)))) + (ert-deftest stream-seq-copy-test () (should (streamp (seq-copy (stream-range)))) (should (= 0 (stream-first (seq-copy (stream-range))))) -- 2.34.1