For Gauche 0.9.6

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

11.34 srfi-158 - Generators and accumulators

Module: srfi-158

This is an enhanced (and upward compatible) version of srfi-121 Generators. In Gauche, all generator procedures are the same as provided in gauche.generator (see Generators).

generator                circular-generator       make-iota-generator
make-range-generator     make-coroutine-generator
list->generator          vector->generator        reverse-vector->generator
string->generator        bytevector->generator
make-for-each-generator  make-unfold-generator
gcons*                   gappend                  gflatten
ggroup                   gmerge                   gmap
gcombine                 gfilter                  gremove
gstate-filter            ggroup                   generator-map->list
gtake                    gdrop                    gtake-while
gdrop-while              gdelete                  gdelete-neighbor-dups
gindex                   gselect                  generator->list
generator->reverse-list  generator-map->list      generator->vector
generator->vector!       generator->string        generator-fold
generator-for-each       generator-find           generator-count
generator-any            generator-every          generator-unfold

Accumulators are opposite of generators. They are procedures that work as consumers. An accumulator takes one argument. When non-eof value is given, the value is stored, and when EOF is given, the accumulated value is returned. How the values are accumulated depends on the accumulator.

Once EOF is given, the accumulator is “finalized”. Subsequent EOF makes it return the same accumulated value. It is undefined if other values are passed after EOF is passed.

The accumulator can be used to parameterize procedures that yield aggregate objects. Consider the following procedure, which takes items from two generators and accumulate them alternatively. (Note that glet* is Gauche’s procedure but not in srfi-158).

(define (intertwine acc gen1 gen2)
  (let loop ()
    (glet* ([a (gen1)]
            [b (gen2)])
      (acc a)
      (acc b)
  (acc (eof-object)))

The procedure can return various type of collections, without knowing the actual type—the passed accumulator determines it.

(intertwine (list-accumulator) (giota 5) (giota 5 100))
  ⇒ (0 100 1 101 2 102 3 103 4 104)
(intertwine (vector-accumulator) (giota 5) (giota 5 100))
  ⇒ #(0 100 1 101 2 102 3 103 4 104)
(intertwine (bytevector-accumulator) (giota 5) (giota 5 100))
  ⇒ #u8(0 100 1 101 2 102 3 103 4 104)

Note: In Gauche, you can also use classes to parameterize returned container types (e.g. map-to), for many collection classes support builder protocol. See Collection framework, for the details. Accumulator has the flexibility that you can provide more than one ways to construct return value on the same type (e.g. forward and reverse list).

Function: make-accumulator kons knil finalizer

[SRFI-158] {srfi-158} Creates and returns an accumulator with a state, whose initial value is knil. When non-EOF value v is passed to the accumulator, kons is called as (kons v state), and its result becomes the new state value. When EOF value is passed, (finalizer state) is called and its result becomes the result of accumulator.

Function: list-accumulator
Function: reverse-list-accumulator

[SRFI-158] {srfi-158} Creates and returns accumulators that return accumulated value as a list, in the accumulated order (list-accumulator) or the reverse order (reverse-list-accumulator).

Function: vector-accumulator
Function: reverse-vector-accumulator
Function: bytevector-accumulator

[SRFI-158] {srfi-158} Returns accumulators that return accumulated value as a fresh vector or bytevector (u8vector), in the accumulated order (vector-accumulator, bytevector-accumulator) or the reverse order (reverse-vector-accumulator). There’s no reverse-bytevector-accumulator.

Function: vector-accumulator! vec at
Function: bytevector-accumulator! bvec at

[SRFI-158] {srfi-158} The vec or bvec argument is a mutable vector or bytevector (u8vector), and is used as a buffer.

Returns an accumlator that stores the accumulated values in the buffer, starting from the index at. It is an error if the accumulator gets more values after the buffer reaches at the end.

Once EOF is passed to the accumulator, vec or bvec is returned, respectively.

Function: string-accumulator

[SRFI-158] {srfi-158} Returns an accumulator that accepts characters and accumulates them to a string.

Function: sum-accumulator
Function: product-accumulator
Function: count-accumulator

[SRFI-158] {srfi-158} Returns accumulators that yield a scalar value.

The accumulator created by sum-accumulator and product-accumulator accepts numbers, and keep adding or multiplying it with the accumulated value (the default value is 0 and 1, respectively).

The accumulator created by count-accumulator accepts any objects and just counting it.

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