Add stream* to complement stream
This commit is contained in:
parent
33717eebaa
commit
c79f646545
|
@ -828,6 +828,12 @@ stream, but plain lists can be used as streams, and functions such as
|
||||||
@racket[empty-stream].
|
@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?]{
|
@defproc[(in-stream [s stream?]) sequence?]{
|
||||||
Returns a sequence that is equivalent to @racket[s].
|
Returns a sequence that is equivalent to @racket[s].
|
||||||
@speed[in-stream "streams"]
|
@speed[in-stream "streams"]
|
||||||
|
|
|
@ -35,6 +35,10 @@
|
||||||
(test 4 stream-ref one-to-four 3)
|
(test 4 stream-ref one-to-four 3)
|
||||||
(test 2 'stream (stream-length (stream 1 (/ 0))))
|
(test 2 'stream (stream-length (stream 1 (/ 0))))
|
||||||
(test 'a 'stream (stream-first (stream 'a (/ 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
|
;; make sure stream operations work on lists
|
||||||
(test #t stream-empty? '())
|
(test #t stream-empty? '())
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
in-stream
|
in-stream
|
||||||
|
|
||||||
stream
|
stream
|
||||||
|
stream*
|
||||||
stream->list
|
stream->list
|
||||||
stream-length
|
stream-length
|
||||||
stream-ref
|
stream-ref
|
||||||
|
@ -58,6 +59,13 @@
|
||||||
((_ hd tl ...)
|
((_ hd tl ...)
|
||||||
(stream-cons hd (stream 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)
|
(define (stream->list s)
|
||||||
(for/list ([v (in-stream s)]) v))
|
(for/list ([v (in-stream s)]) v))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user