util.stream- Stream library
This module provides a library of lazy streams, including the functions and syntaxes defined in srfi-40.
#t iff obj is a stream created by a procedure
[SRFI-40] The singleton instance of NULL stream.
[SRFI-40] A fundamental constructor of a stream. Adds object to the head of a stream, and returns a new stream.
#t iff obj is the null stream.
#t iff obj is a non-null stream.
[SRFI-40] Returns the first element of the stream s.
[SRFI-40] Returns the remaining elements of the stream s, as a stream.
[SRFI-40] Returns a stream which is a delayed form of expr.
As a rule of thumb, any stream-producing functions should
wrap the resulting expression by
[SRFI-40] Returns a new stream whose elements are obj ….
[SRFI-40] Creates n streams related each other, whose contents are generated by generator and seed.
The generator is called with the current seed value,
(generator seed) => seed result_0 result_1 … result_n-1
The first value is to be the next seed value. Result_k must be one of the following forms:
val will be the next car of k-th stream.
No new information for k-th stream.
The end of k-th stream has been reached.
The following example creates two streams, the first one produces an infinite series of odd numbers and the second produces evens.
gosh> (define-values (s0 s1) (stream-unfoldn (lambda (i) (values (+ i 2) ;; next seed (list i) ;; for the first stream (list (+ i 1)))) ;; for the second stream 0 2)) #<undef> gosh> (stream->list (stream-take s0 10)) (0 2 4 6 8 10 12 14 16 18) gosh> (stream->list (stream-take s1 10)) (1 3 5 7 9 11 13 15 17 19)
[SRFI-40] Returns a new stream, whose elements are calculated by applying func to each element of streams.
[SRFI-40] Applies func for each element of streams. Terminates if one of streams reaches the end.
[SRFI-40] Returns a new stream including only elements passing pred?.
The following procedures are taken from the library written by Alejandro Forero Cuervo for Chicken Scheme. They follow the naming conventions of srfi-1 (List library).
(stream-cons b a). Just for convenience.
Creates a new stream which appends elt … before stream.
Creates a new stream of n elements of init.
If init is omitted,
#f is used.
Specifying a negative number to n creates an infinite stream.
Creates a new stream of n elements. The k-th element is obtained by applying init-proc to k. Specifying a negative number to n creates an infinite stream. Creates a new stream of integers, starting from start and incrementing step. The length of stream is count if it is positive, or infinite if count is negative. The default values of start and step are 0 an 1, respectively.
Returns a stream which is a result of applying
(format fmt arg …).
Converts a stream to a list or a string. All of stream’s
forced; if stream is infinite, these
procedures won’t terminate. For
all stream must be characters, or an error is signaled.
Converts a list to a stream of its elements.
Convers a string to a stream of characters. If an optional stream is given, it becomes the tail of the resulting stream.
(stream->list (string->stream "abc" (list->stream ’(1 2 3)))) ⇒ (#\a #\b #\c 1 2 3)
Creates a stream, whose elements consist of the items
read from the input port iport.
The default iport is the current input port.
The default reader is
The result stream terminates at the point where reader returns EOF (EOF itself is not included in the stream). If closer is given, it is called with iport as an argument just after reader reads EOF.
A generic procedure to turn an internal iterator iter into a stream of iterated results.
The iter argument is a procedure that takes two arguments, next and end, where next is a procedure that takes one argument and end is a thunk. Iter is supposed to iterate over some set and call next for each argument, then call end to indicate the end of the iteration. Here’s a contrived example:
(stream->list (iterator->stream (lambda (next end) (for-each next '(1 2 3 4 5)) (end)))) ⇒ (1 2 3 4 5)
iterator->stream uses the “inversion of iterator”
technique, so that iter only iterates to the element that
are needed by the stream. Thus iter can iterate over
an infinite set. In the following example, iter is
an infinite loop calling next with increasing integers,
but only the first 10 elements are calculated because of
(stream->list (stream-take (iterator->stream (lambda (next end) (let loop ((n 0)) (next n) (loop (+ n 1))))) 10)) ⇒ (0 1 2 3 4 5 6 7 8 9)
Splits stream where its element equals to
returns a stream of splitted streams.
(stream->list (stream-map stream->string (stream-lines (string->stream "abc\ndef\nghi")))) ⇒ ("abc" "def" "ghi")
Returns true iff each corresponding element of stream … are the same in terms of elt=. This procedure won’t terminate if any of streams is infinite.
Compares initial elements of stream against a list prefix by elt=. Only as many elements of stream as prefix has are checked.
(stream-caar s) =
(stream-car (stream-car s)) etc.
Returns the pos-th element in the stream. Pos must be a nonnegative exact integer.
(stream-first s) =
(stream-ref s 0) etc.
Returns a new stream that consists of the first count elements
of the given stream. If the given stream has less than
count elements, the stream returned by
would raise an error when the elements beyond the original stream
is accessed. On the other hand, the stream returned by
stream-take-safe will return a shortened stream when
the given stream has less than count elements.
(stream->list (stream-take (stream-iota -1) 10)) ⇒ (0 1 2 3 4 5 6 7 8 9) (stream-take (stream 1 2) 5) ⇒ stream (stream->list (stream-take (stream 1 2) 5)) ⇒ error (stream->list (stream-take-safe (stream 1 2) 5)) ⇒ (1 2)
Returns a new stream that consists of the elements in the given
stream except the first count elements.
If the given stream has less than count elements,
stream-drop returns a stream that raises an error
if its element is accessed, and
returns an empty stream.
Returns a new stream in which element is inserted between elements of stream.