From 7e666b4b4513196880c6b4a76e5bb6490d31e518 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Mon, 5 Dec 2011 15:55:56 -0500 Subject: [PATCH] add `stream' to `racket/stream' based on a pull request from Dan King --- collects/racket/stream.rkt | 10 +++++++++- collects/scribblings/reference/sequences.scrbl | 5 +++++ collects/tests/racket/stream.rktl | 9 +++++++++ doc/release-notes/racket/HISTORY.txt | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/collects/racket/stream.rkt b/collects/racket/stream.rkt index 9f854c2e02..a650070b3e 100644 --- a/collects/racket/stream.rkt +++ b/collects/racket/stream.rkt @@ -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)) diff --git a/collects/scribblings/reference/sequences.scrbl b/collects/scribblings/reference/sequences.scrbl index baa2a2fa72..d3ecdfb637 100644 --- a/collects/scribblings/reference/sequences.scrbl +++ b/collects/scribblings/reference/sequences.scrbl @@ -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"]} diff --git a/collects/tests/racket/stream.rktl b/collects/tests/racket/stream.rktl index 45b607400b..884f01f91c 100644 --- a/collects/tests/racket/stream.rktl +++ b/collects/tests/racket/stream.rktl @@ -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) diff --git a/doc/release-notes/racket/HISTORY.txt b/doc/release-notes/racket/HISTORY.txt index effbca6cfb..4ff38aae7b 100644 --- a/doc/release-notes/racket/HISTORY.txt +++ b/doc/release-notes/racket/HISTORY.txt @@ -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