Add stream* to complement stream

This commit is contained in:
Alexis King 2015-05-22 20:50:55 -07:00 committed by Matthew Flatt
parent 33717eebaa
commit c79f646545
3 changed files with 18 additions and 0 deletions

View File

@ -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"]

View File

@ -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? '())

View File

@ -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))