For Gauche 0.9.8

Next: , Previous: , Up: Library modules - Utilities   [Contents][Index]

12.71 - Stream library


This module provides a library of lazy streams, including the functions and syntaxes defined in srfi-40.

Function: stream? obj

[SRFI-40] {} Returns #t iff obj is a stream created by a procedure of

Variable: stream-null

[SRFI-40] {} The singleton instance of NULL stream.

Macro: stream-cons object stream

[SRFI-40] {} A fundamental constructor of a stream. Adds object to the head of a stream, and returns a new stream.

Function: stream-null? obj

[SRFI-40] {} Returns #t iff obj is the null stream.

Function: stream-pair? obj

[SRFI-40] {} Returns #t iff obj is a non-null stream.

Function: stream-car s

[SRFI-40] {} Returns the first element of the stream s.

Function: stream-cdr s

[SRFI-40] {} Returns the remaining elements of the stream s, as a stream.

Macro: stream-delay expr

[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 stream-delay.

Function: stream obj …

[SRFI-40] {} Returns a new stream whose elements are obj ….

Function: stream-unfoldn generator seed n

[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, and returns n+1 values:

(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))
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)
Function: stream-map func . streams

[SRFI-40] {} Returns a new stream, whose elements are calculated by applying func to each element of streams.

Function: stream-for-each func . streams

[SRFI-40] {} Applies func for each element of streams. Terminates if one of streams reaches the end.

Function: stream-filter pred? stream

[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).

Function: stream-xcons a b

{} (stream-cons b a). Just for convenience.

Function: stream-cons* elt … stream

{} Creates a new stream which appends elt … before stream.

Function: make-stream n :optional init

{} 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.

Function: stream-tabulate n init-proc

{} 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.

Function: stream-iota count :optional start step

{} 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.

Function: stream-format fmt arg …

{} Returns a stream which is a result of applying string->stream to (format fmt arg …).

Function: stream->list stream
Function: stream->string stream

{} Converts a stream to a list or a string. All of stream’s elements are forced; if stream is infinite, these procedures won’t terminate. For stream->string, all stream must be characters, or an error is signaled.

Function: list->stream list

{} Converts a list to a stream of its elements.

Function: string->stream string :optional stream

{} 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)

Function: port->stream :optional iport reader closer

{} 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 read-char.

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.

Function: iterator->stream iter

{} 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:

  (lambda (next end) (for-each next '(1 2 3 4 5)) (end))))
 ⇒ (1 2 3 4 5)

Internally 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-take:

   (lambda (next end)
     (let loop ((n 0)) (next n) (loop (+ n 1)))))
 ⇒ (0 1 2 3 4 5 6 7 8 9)
Function: stream-lines stream

{} Splits stream where its element equals to #\n, and returns a stream of splitted streams.

 (stream-map stream->string
             (stream-lines (string->stream "abc\ndef\nghi"))))
 ⇒ ("abc" "def" "ghi")
Function: stream= elt= stream …

{} 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.

Function: stream-prefix= stream prefix :optional elt=

{} Compares initial elements of stream against a list prefix by elt=. Only as many elements of stream as prefix has are checked.

Function: stream-caar s
Function: stream-cadr s

Function: stream-cdddar s
Function: stream-cddddr s

{} (stream-caar s) = (stream-car (stream-car s)) etc.

Function: stream-ref stream pos

{} Returns the pos-th element in the stream. Pos must be a nonnegative exact integer.

Function: stream-first s
Function: stream-second s
Function: stream-third s
Function: stream-fourth s
Function: stream-fifth s
Function: stream-sixth s
Function: stream-seventh s
Function: stream-eighth s
Function: stream-ninth s
Function: stream-tenth s

{} (stream-first s) = (stream-ref s 0) etc.

Function: stream-take stream count
Function: stream-take-safe stream count

{} 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 stream-take 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)
Function: stream-drop stream count
Function: stream-drop-safe stream count

{} 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 stream-drop-safe returns an empty stream.

Function: stream-intersperse stream element

{} Returns a new stream in which element is inserted between elements of stream.

Function: stream-split stream pred


Function: stream-last stream


Function: stream-last-n stream count


Function: stream-butlast stream


Function: stream-butlast-n stream count


Function: stream-length stream


Function: stream-length>= stream n


Function: stream-append stream …


Function: stream-concatenate streams


Function: stream-reverse stream :optional tail-stream


Function: stream-count pred stream …


Function: stream-remove pred stream


Function: stream-partition pred stream


Function: stream-find pred stream


Function: stream-find-tail pred stream


Function: stream-take-while pred stream


Function: stream-drop-while pred stream


Function: stream-span pred stream


Function: stream-break pred stream


Function: stream-any pred stream …


Function: stream-every pred stream …


Function: stream-index pred stream …


Function: stream-member obj stream :optional elt=
Function: stream-memq obj stream
Function: stream-memv obj stream


Function: stream-delete obj stream :optional elt=


Function: stream-delete-duplicates stream :optional elt=


Function: stream-grep re stream


Function: write-stream stream :optional oport writer


Next: , Previous: , Up: Library modules - Utilities   [Contents][Index]