stream*: allow 1 argument
This commit is contained in:
parent
2670a932f8
commit
710cbd25bf
|
@ -1073,8 +1073,8 @@ 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]
|
||||
@defform[(stream* expr ... rest-expr)]{
|
||||
A shorthand for nested @racket[stream-cons]es, but the @racket[rest-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.
|
||||
|
||||
|
|
|
@ -35,10 +35,14 @@
|
|||
(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 1 'stream* (stream-first (stream* (stream 1))))
|
||||
(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)))))))
|
||||
(err/rt-test (stream* 2) exn:fail:contract? "stream*")
|
||||
(test #true 'stream* (stream? (stream* 1 0)))
|
||||
(err/rt-test (stream-length (stream* 1 2)) exn:fail:contract? "stream*")
|
||||
|
||||
;; make sure stream operations work on lists
|
||||
(test #t stream-empty? '())
|
||||
|
|
|
@ -70,11 +70,16 @@
|
|||
|
||||
(define-syntax stream*
|
||||
(syntax-rules ()
|
||||
[(_ hd tl)
|
||||
(stream-cons hd tl)]
|
||||
[(_ tl)
|
||||
(assert-stream? 'stream* tl)]
|
||||
[(_ hd tl ...)
|
||||
(stream-cons hd (stream* tl ...))]))
|
||||
|
||||
(define (assert-stream? who st)
|
||||
(if (stream? st)
|
||||
st
|
||||
(raise-argument-error who "stream?" st)))
|
||||
|
||||
(define (stream->list s)
|
||||
(for/list ([v (in-stream s)]) v))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user