4.17.2 Streams (original) (raw)

4.17.2 Streams🔗

A stream is a kind of sequence that supports functional iteration via stream-first andstream-rest. The stream-cons form constructs a lazy stream, but plain lists can be used as streams, and functions such asin-range and in-naturals also create streams.

Returns #t if v can be used as a stream,#f otherwise.

Returns #t if s has no elements, #fotherwise.

Returns the value(s) of the first element in s.

Returns a stream that is equivalent to s without its first element.

(stream-cons first-expr rest-expr)
(stream-cons #:eager first-expr rest-expr)
(stream-cons first-expr #:eager rest-expr)
(stream-cons #:eager first-expr #:eager rest-expr)

Produces a stream whose first element is determined byfirst-expr and whose rest is determined byrest-expr.

If first-expr is not preceded by #:eager, thenfirst-expr is not evaluated immediately. Instead,stream-first on the result stream forces the evaluation offirst-expr (once) to produce the first element of the stream. If evaluating first-expr raises an exception or tries to force itself, then an exn:fail:contract exception is raised, and future attempts to force evaluation will trigger another exception.

If rest-expr is not preceded by #:eager, thenrest-expr is not evaluated immediately. Instead,stream-rest on the result stream produces another stream that is like the one produced by (stream-lazy rest-expr).

The first element of the stream as produced by first-exprcan be multiple values. The rest-expr must produce a stream when it is evaluated, otherwise the exn:fail:contract? exception is raised.

Changed in version 8.0.0.12 of package base: Added #:eager options.
Changed in version 8.8.0.7: Changed to allow multiple values.

(stream-lazy stream-expr)
(stream-lazy #:who who-expr stream-expr)

Similar to (delay stream-expr), but the result is a stream instead of a promise, and stream-expr must produce a stream when it is eventually forced. The stream produced bystream-lazy has the same content as the stream produced bystream-expr; that is, operations like stream-firston the result stream will force stream-expr and retry on its result.

If evaluating stream-expr raises an exception or tries to force itself, then an exn:fail:contract exception is raised, and future attempts to force evaluation will trigger another exception.

If who-expr is provided, it is evaluated when constructing the delayed stream. If stream-expr later produces a value that is not a stream, and if who-expr produced a symbol value, then the symbol is used for the error message.

Added in version 8.0.0.12 of package base.

Forces the evaluation of a delayed stream from stream-lazy, from the stream-rest of a stream-cons, etc., returning the forced stream. If s is not a delayed stream, then s is returned.

Normally, stream-force is not needed, because operations like stream-first, stream-rest, andstream-empty? force a delayed stream as needed. In rare cases, stream-force can be useful to reveal the underlying implementation of a stream (e.g., a stream that is an instance of a structure type that has the prop:stream property).

Added in version 8.0.0.12 of package base.

(stream elem-expr ...)
elem-expr = (values single-expr ...) | single-expr

A shorthand for nested stream-conses ending withempty-stream. As a match pattern, streammatches a stream with as many elements as elem-exprs, and each element must match the corresponding elem-expr pattern. The pattern elem-expr can be (values single-expr ...), which matches against multiple valued elements in the stream.

Changed in version 8.8.0.7 of package base: Changed to allow multiple values.

(stream* elem-expr ... tail-expr)

A shorthand for nested stream-conses, but the tail-expr must produce a stream when it is forced, and that stream is used as the rest of the stream instead ofempty-stream. Similar to list* but for streams. As a match pattern, stream* is similar to a stream pattern, but the tail-expr pattern matches the “rest” of the stream after the last elem-expr.

Added in version 6.3 of package base.
Changed in version 8.0.0.12: Changed to delay rest-expr even if zero exprs are provided.
Changed in version 8.8.0.7: Changed to allow multiple values.

Returns a sequence that is equivalent to s.

An in-stream application can provide better performance for streams iteration when it appears directly in a for clause.

See for for information on the reachability of stream elements during an iteration.

Changed in version 6.7.0.4 of package base: Improved element-reachability guarantee for streams in for.

A stream with no elements.

Returns a list whose elements are the elements of s, each of which must be a single value. If s is infinite, this function does not terminate.

Returns the number of elements of s. If s is infinite, this function does not terminate.

In the case of lazy streams, this function forces evaluation only of the sub-streams, and not the stream’s elements.

Returns the ith element of s (which may be multiple values).

Returns a stream equivalent to s, except that the firsti elements are omitted.

In case extracting elements from s involves a side effect, they will not be extracted until the first element is extracted from the resulting stream.

Returns a stream of the first i elements of s.

(stream-append s ...) → stream?
s : stream?

Returns a stream that contains all elements of each stream in the order they appear in the original streams. The new stream is constructed lazily, while the last given stream is used in the tail of the result.

Returns a stream that contains f applied to each element ofs. The new stream is constructed lazily.

Returns #t if f returns a true result on every element of s. If s is infinite and fnever returns a false result, this function does not terminate.

Returns #t if f returns a true result on some element of s. If s is infinite and fnever returns a true result, this function does not terminate.

Applies f to each element of s. If s is infinite, this function does not terminate.

Folds f over each element of s with i as the initial accumulator. If s is infinite, this function does not terminate. The f function takes the accumulator as its first argument and the next stream element as its second.

Returns the number of elements in s for which freturns a true result. If s is infinite, this function does not terminate.

Returns a stream whose elements are the elements of s for which f returns a true result. Although the new stream is constructed lazily, if s has an infinite number of elements where f returns a false result, then operations on this stream will not terminate during the infinite sub-stream.

Returns a stream whose elements are the elements of s, but with e between each pair of elements in s. The new stream is constructed lazily.

(for/stream (for-clause ...) body-or-break ... body)
(for*/stream (for-clause ...) body-or-break ... body)

Iterates like for/list and for*/list, respectively, but the results are lazily collected into a stream instead of a list.

Unlike most for forms, these forms are evaluated lazily, so eachbody will not be evaluated until the resulting stream is forced. This allows for/stream and for*/stream to iterate over infinite sequences, unlike their finite counterparts.

Examples:

Added in version 6.3.0.9 of package base.
Changed in version 8.8.0.7: Changed to allow multiple values.

To supply method implementations, the #:methods keyword should be used in a structure type definition. The following three methods must be implemented:

Examples:

> (define l1 (list-stream '(1 2)))
> (stream? l1)
#t
> (stream-first l1)
1

Changed in version 8.7.0.5 of package base: Added a check so that omitting any ofstream-empty?, stream-first, and stream-restis now a syntax error.

A structure type property used to define custom extensions to the stream API. Using the prop:stream property is discouraged; use the gen:stream generic interfaceinstead. Accepts a vector of three procedures taking the same arguments as the methods in gen:stream.

Returns a contract that recognizes streams. All elements of the stream must matchc.

If the c argument is a flat contract or a chaperone contract, then the result will be a chaperone contract. Otherwise, the result will be an impersonator contract.

When an stream/c contract is applied to a stream, the result is noteq? to the input. The result will be either a chaperone orimpersonator of the input depending on the type of contract.

Contracts on streams are evaluated lazily by necessity (since streams may be infinite). Contract violations will not be raised until the value in violation is retrieved from the stream. As an exception to this rule, streams that are lists are checked immediately, as if c had been used withlistof.

If a contract is applied to a stream, and that stream is subsequently used as the tail of another stream (as the second parameter to stream-cons), the new elements will not be checked with the contract, but the tail’s elements will still be enforced.

Added in version 6.1.1.8 of package base.