diff --git a/pkgs/racket-doc/scribblings/reference/sequences.scrbl b/pkgs/racket-doc/scribblings/reference/sequences.scrbl index 7647bfcb8b..aef9e56f83 100644 --- a/pkgs/racket-doc/scribblings/reference/sequences.scrbl +++ b/pkgs/racket-doc/scribblings/reference/sequences.scrbl @@ -828,6 +828,12 @@ stream, but plain lists can be used as streams, and functions such as @racket[empty-stream]. } +@defform[(stream* expr ...)]{ + A shorthand for nested @racket[stream-cons]es, but the final @racket[expr] + must be a stream, and it is used as the rest of the stream instead of + @racket[empty-stream]. Similar to @racket[list*] but for streams. +} + @defproc[(in-stream [s stream?]) sequence?]{ Returns a sequence that is equivalent to @racket[s]. @speed[in-stream "streams"] diff --git a/pkgs/racket-test-core/tests/racket/stream.rktl b/pkgs/racket-test-core/tests/racket/stream.rktl index 3a75e12ca0..3b54420953 100644 --- a/pkgs/racket-test-core/tests/racket/stream.rktl +++ b/pkgs/racket-test-core/tests/racket/stream.rktl @@ -35,6 +35,10 @@ (test 4 stream-ref one-to-four 3) (test 2 'stream (stream-length (stream 1 (/ 0)))) (test 'a 'stream (stream-first (stream 'a (/ 0)))) +(test 2 'stream* (stream-length (stream* 1 (stream (/ 0))))) +(test 'a 'stream* (stream-first (stream* 'a (stream (/ 0))))) +(test 4 'stream* (stream-length (stream* 'a 'b 'c (stream (/ 0))))) +(test 'c 'stream* (stream-first (stream-rest (stream-rest (stream* 'a 'b 'c (stream (/ 0))))))) ;; make sure stream operations work on lists (test #t stream-empty? '()) diff --git a/racket/collects/racket/stream.rkt b/racket/collects/racket/stream.rkt index 9bf6f42ae7..32dc6f6e48 100644 --- a/racket/collects/racket/stream.rkt +++ b/racket/collects/racket/stream.rkt @@ -27,6 +27,7 @@ in-stream stream + stream* stream->list stream-length stream-ref @@ -58,6 +59,13 @@ ((_ hd tl ...) (stream-cons hd (stream tl ...))))) +(define-syntax stream* + (syntax-rules () + [(_ hd tl) + (stream-cons hd tl)] + [(_ hd tl ...) + (stream-cons hd (stream* tl ...))])) + (define (stream->list s) (for/list ([v (in-stream s)]) v))