For Gauche 0.9.15Search (procedure/syntax/module):

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

11.55 srfi.221 - Generator/accumulator sub-library

Module: srfi.221

This srfi adds several utility procedures to the generator library (see gauche.generator - Generators, for Gauche’s native support of generators; see scheme.generator - R7RS generators, for R7RS-large spec).

Function: gcompose-left constructor operation …
Function: gcompose-right constructor operation …

[SRFI-221]{srfi.221} The constructor argument is a thunk that returns a generator. Each operation is a procedure that takes a generator and returns a generator.

These procedures creates and returns a generator that is a composition of given operations on top of the one constructed by constructor. gcompose-left applies operations left to right (left-associative), while gcompose-right does right to left (right-associative).

(use gauche.generator)
(use srfi.221)

(generator->list
  (gcompose-left
    (cut make-iota-generator 100)
    (cut gfilter even? <>)
    (cut ggroup <> 5)))
 ⇒ ((0 2 4 6 8) (10 12 14 16 18) (20 22 24 26 28)
     (30 32 34 36 38) (40 42 44 46 48) (50 52 54 56 58)
     (60 62 64 66 68) (70 72 74 76 78) (80 82 84 86 88)
     (90 92 94 96 98))

;; The same result can be obtained with:
(generator->list
  (gcompose-right
    (cut ggroup <> 5)
    (cut gfilter even? <>)
    (cut make-iota-generator 100)))
Function: accumulate-generated-values accumulator generator

[SRFI-221]{srfi.221} Calls generator repeatedly, accumulating the values into accumulator, until generator is exhausted. Then it retrieves and returns the accumulated value. See scheme.generator - R7RS generators, for the details of accumulators.

It is a generalization of generator->list type of converter; you can pass an accumulator that returns the desired type of result.

Function: genumerate gen

[SRFI-221]{srfi.221} Returns a generator that yields pairs, each of which consists of an exact integer count and the value yielded by gen. The count starts from 0 and incremented.

(generator->list (genumerate (list->generator '(a b c d e))))
  ⇒ ((0 . a) (1 . b) (2 . c) (3 . d) (4 . e))
Function: gchoice choice-gen source-gen …

[SRFI-221]{srfi.221} All arguments are generators. Returns a generator that yields a value from one of source-gens, according to choice-gen.

The choice-gen must be a generator that yields exact integers between 0 and one minus the number of source-gens.

Each time the resulting generator is invoked, choice-gen is called. If it is exhausted, the resulting generator is also exhausted.

Otherwise, the source-gen indexed by the result of choice-gen is called, and if it isn’t exhausted, the value is returned.

If the selected source-gen is exhausted, choice-gen is retried until non-exhausted source-gen yields a value. If all source-gen is exhausted, the resulting generator is also exhausted.

Function: stream->generator stream

[SRFI-221]{srfi.221} Returns a generator that yields each element of a lazy stream stream. Lazy streams are defined in R7RS-large scheme.stream (see scheme.stream - R7RS stream), which is a subset of Gauche’s util.stream (see util.stream - Stream library).

Function: generator->stream generator

[SRFI-221]{srfi.221} Returns a lazy stream whose elements consist of the values generated by generator Lazy streams are defined in R7RS-large scheme.stream (see scheme.stream - R7RS stream), which is a subset of Gauche’s util.stream (see util.stream - Stream library).

See also generator->lseq (see Lazy sequences), which returns a lazy sequence defined in R7RS-large scheme.lseq. In Gauche, lazy sequences are integrated to ordinary lists and more lightweight than lazy streams.


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


For Gauche 0.9.15Search (procedure/syntax/module):