Previous: Queues, Up: Compound Data Types


22.9 Streams

To use procedures for creating and working with streams of objects, evaluate the form:

     (use-modules (ice-9 streams))
Historical Note: The basic stream operations are inspired
by (i.e. ripped off) Scheme48's `stream' package, modulo
stream-empty? -> stream-null? renaming.

For procedures that convert streams to lists or vectors, if
the stream has infinite length the procedure will not terminate.
— Scheme Procedure: make-stream producer initial-state

Return a stream object controlled by producer and initial-state. producer is a procedure of one argument, the current state. It should return either a pair or an atom (i.e. anything that is not a pair). If producer returns a pair, then the car of the pair is the stream's head value, and the cdr is the state to be fed to producer later. If producer returns a non-pair, then the stream is considered depleted.

— Scheme Procedure: stream-car stream

Return the first element in stream. This is analogous to (car list).

— Scheme Procedure: stream-cdr stream

Return the first tail of stream. This is analogous to (cdr list).

— Scheme Procedure: stream-null? stream

Return #t if stream is the end-of-stream marker; otherwise return #f. This is analogous to (null? list), but should be used whenever testing for the end of a stream.

— Scheme Procedure: list->stream list

Return a new stream whose elements are the elements of list.

— Scheme Procedure: vector->stream vector

Return a new stream whose elements are the elements of vector.

— Scheme Procedure: port->stream port read

Return a new stream whose elements are obtained by reading from port.

— Scheme Procedure: stream->list stream

Return a new list whose elements are the elements of stream.

— Scheme Procedure: stream->reversed-list stream

Return a new list whose elements are the elements of stream, in reversed order.

— Scheme Procedure: stream->list&length stream

Return two values: a new list whose elements are the elements of stream, and the length of the list.

— Scheme Procedure: stream->reversed-list&length stream

Return two values: a new list whose elements are the elements of stream in reversed order, and the length of the list.

— Scheme Procedure: stream->vector stream

Return a vector whose elements are the elements of stream.

— Scheme Procedure: stream-fold proc init stream0 [stream1...]

Apply proc to successive elements of the given streams and to the value of the previous invocation (init on the first invocation). Return the last result from proc.

proc must take one more than the number of streams, like this:

          (proc car0 ... init)

NOTE: The init argument is last, not first. (This was chosen to be consistent the fold procedures from SRFI-1.)

— Scheme Procedure: stream-for-each proc stream0 [stream1...]

Apply proc to each car of streams. The return value is unspecified.

— Scheme Procedure: stream-map proc stream0 [stream1...]

Return a newly allocated stream, each element being the result of applying proc to the corresponding elements of the streams as its arguments.