Next: streams fold, Previous: streams range, Up: streams [Index]
Provide the syntax of stream comprehensions, which generate streams by means of looping expressions. The result is a stream of objects of the type returned by expr. There are four types of clause arguments:
(var in stream-expr)
Loop over the elements of stream-expr, in order from the start of
the stream, binding each element of the stream in turn to var.
stream-from
and stream-range
are frequently useful as
generators for stream-expr.
(var is expr)
Bind var to the value obtained by evaluating expr.
(pred expr)
Include in the output stream only those elements x for which
(pred x)
is true.
The scope of variables bound in the stream comprehension is the clauses to the right of the binding clause (but not the binding clause itself) plus the result expression.
When two or more generators are present, the loops are processed as if
they are nested from left to right; that is, the rightmost generator
varies fastest. A consequence of this is that only the first generator
may be infinite and all subsequent generators must be finite. If no
generators are present, the result of a stream comprehension is a stream
containing the result expression; thus, (stream-of 1)
produces a
finite stream containing only the element 1
.
(stream-of (* x x) (x in (stream-range 0 10)) (even? x)) ⇒ 0 4 16 36 64 (stream-of (list a b) (a in (stream-range 1 4)) (b in (stream-range 1 3))) ⇒ (1 1) (1 2) (2 1) (2 2) (3 1) (3 2) (stream-of (list i j) (i in (stream-range 1 5)) (j in (stream-range (+ i 1) 5))) ⇒ (1 2) (1 3) (1 4) (2 3) (2 4) (3 4)
Next: streams fold, Previous: streams range, Up: streams [Index]