stream*: allow 1 argument

This commit is contained in:
Ben Greenman 2018-08-16 00:15:47 -04:00
parent 2670a932f8
commit 710cbd25bf
3 changed files with 13 additions and 4 deletions

View File

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

View File

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

View File

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