chickadee » srfi-121 » generator-fold

generator-fold proc seed gen1 gen2 ...procedure

Works like SRFI 1 fold on the values generated by the generator arguments.

When one generator is given, for each value v generated by gen, proc is called as (proc v r), where r is the current accumulated result; the initial value of the accumulated result is seed, and the return value from proc becomes the next accumulated result. When gen is exhausted, the accumulated result at that time is returned from generator-fold.

When more than one generator is given, proc is invoked on the values returned by all the generator arguments followed by the current accumulated result. The procedure terminates when any of the genn generators is exhausted, at which time all the others are in an undefined state.

(with-input-from-string "a b c d e"
  (lambda () (generator-fold cons 'z read)))
 ⇒ (e d c b a . z)