add stream' to racket/stream'

based on a pull request from Dan King
This commit is contained in:
Matthew Flatt 2011-12-05 15:55:56 -05:00
parent 90acf8a36f
commit 7e666b4b45
4 changed files with 24 additions and 1 deletions

View File

@ -14,6 +14,7 @@
prop:stream
in-stream
stream
stream->list
stream-length
stream-ref
@ -27,7 +28,14 @@
stream-filter
stream-add-between
stream-count)
(define-syntax stream
(syntax-rules ()
((_)
empty-stream)
((_ hd tl ...)
(stream-cons hd (stream tl ...)))))
(define (stream->list s)
(for/list ([v (in-stream s)]) v))

View File

@ -595,6 +595,11 @@ The first element of the stream as produced by @racket[first-expr]
must be a single value. The @racket[rest-expr] must produce a stream
when it is evaluated, otherwise the @exnraise[exn:fail:contract?].}
@defform[(stream expr ...)]{
A shorthand for nested @racket[stream-cons]es ending with
@racket[empty-stream].}
@defproc[(in-stream [s stream?]) sequence?]{
Returns a sequence that is equivalent to @racket[s].
@speed[in-stream "streams"]}

View File

@ -10,6 +10,7 @@
(test #f stream? '#(1 2))
(test 1 'stream-length (stream-length (stream-cons 1 empty-stream)))
(test 1 'stream-length (stream-length (stream 1)))
(define infinite-ones (stream-cons 1 infinite-ones))
@ -27,4 +28,12 @@
(test "hello" stream-first (sequence->stream (in-producer (lambda () "hello") (void))))
(test 65 stream-first (sequence->stream (in-port read-byte (open-input-string "A"))))
(define one-to-four (stream 1 2 3 4))
(test 1 stream-first one-to-four)
(test 2 stream-ref one-to-four 1)
(test 3 stream-ref one-to-four 2)
(test 4 stream-ref one-to-four 3)
(test 2 'stream (stream-length (stream 1 (/ 0))))
(test 'a 'stream (stream-first (stream 'a (/ 0))))
(report-errs)

View File

@ -2,6 +2,7 @@ Version 5.2.0.5
Cross-module inlining of trivial functions, plus map, for-each,
andmap, and ormap; 'compiler-hint:cross-module-inline hint
compiler/zo-structs: added inline-variant
racket/stream: added stream constructor
Version 5.2.0.4
Regexps are `equal?' when they have the same source [byte] string