Lazy generators: produce-on-demand sequences with the usual functional toolbox.
Lazy generators (unfold-based): build (range/iterate/repeat/from-list/of),
transform lazily (map/filter/take/drop/zip/scan/…), drive eagerly
(->list/fold/sum/find/…). Transformers return a Gen, so chaining never
builds an intermediate list.
GenA lazy generator: a step function over a state, producing values on demand.
Build with range / range-by / count-from / repeat / iterate / from-list / from-seq / of, or the
makeprimitive.
Transformers (%map filter take drop take-while drop-while enumerate zip zip-with scan) are lazy – each returns a new Gen.
Consumers (->list ->vector for-each fold reduce count sum product any? all? none? find ref first last min max empty?) drive it.
count-from / repeat / iterate are INFINITE – bound them with take / take-while before any consumer.
stepMember: data carried by a Gen instance.
stateMember: data carried by a Gen instance.
(Gen make step state)A generator from a raw step function and an initial state – the primitive the rest build on.
Block form: (Gen make (x) body … state) – or (i x) for the 0-based index, then the element.
Parameters:
step : CALLABLE — (step state) -> (value . next-state) |
() |
ANY — Initial stateReturns: GEN — Generator
(Gen range start stop)Integers from start up to (not including) stop.
Parameters:
INT — Start (inclusive)INT — Stop (exclusive)Returns: GEN — Generator
Examples:
((Gen range 0 4) ->list) => (0 1 2 3)(Gen range-by start stop by)Integers from start toward stop, stepping by by.
Parameters:
INT — StartINT — Stop (exclusive)INT — Step, may be negative but not zeroReturns: GEN — Generator
Examples:
((Gen range-by 0 10 3) ->list) => (0 3 6 9)(Gen count-from start)INFINITE: start, start+1, start+2, … Bound with take / take-while.
Parameters:
INT — First valueReturns: GEN — Infinite generator
Examples:
(((Gen count-from 1) take 3) ->list) => (1 2 3)(Gen repeat x)INFINITE: x, x, x, … Bound with take.
Parameters:
ANY — Value to repeatReturns: GEN — Infinite generator
Examples:
(((Gen repeat 7) take 3) ->list) => (7 7 7)(Gen iterate f x)INFINITE: x, (f x), (f (f x)), … Bound with take / take-while.
Block form: (Gen iterate (x) body … x) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — Successor functionANY — SeedReturns: GEN — Infinite generator
Examples:
(((Gen iterate (fn (_ n) (* n 2)) 1) take 4) ->list) => (1 2 4 8)(Gen from-seq v)A generator over any iterable value, driven functionally by the C iterator steps: the state is an Iter, stepped without mutation.
Parameters:
ANY — Iterable value: list, vector, string, or instanceReturns: GEN — Generator
Examples:
((Gen from-seq (Vector of 1 2 3)) ->list) => (1 2 3)(Gen from-list lst)A generator over a list’s elements.
Parameters:
LIST — Source listReturns: GEN — Generator
Examples:
((Gen from-list (list 1 2 3)) ->list) => (1 2 3)(Gen of . args)A generator over the given values.
Parameters:
ANY — ValuesReturns: GEN — Generator
Examples:
((Gen of 1 2 3) ->list) => (1 2 3)(%next)Instance method: called on a Gen instance.
(map f)A generator that applies f to each value (lazy).
Instance method: called on a Gen instance.
Block form: (map (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — Applied to each valueReturns: GEN — Generator
Examples:
(((Gen range 0 4) map (fn (_ x) (* x x))) ->list) => (0 1 4 9)(filter p)A generator of the values for which p holds (lazy).
Instance method: called on a Gen instance.
Block form: (filter (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — PredicateReturns: GEN — Generator
Examples:
(((Gen range 0 6) filter (fn (_ x) (< x 3))) ->list) => (0 1 2)(take n)A generator of at most the first n values (lazy).
Instance method: called on a Gen instance.
Parameters:
INT — How manyReturns: GEN — Generator
Examples:
(((Gen count-from 0) take 3) ->list) => (0 1 2)(drop n)A generator that skips the first n values (lazy).
Instance method: called on a Gen instance.
Parameters:
INT — How many to skipReturns: GEN — Generator
Examples:
(((Gen range 0 5) drop 2) ->list) => (2 3 4)(take-while p)Values up to (not including) the first for which p fails (lazy).
Instance method: called on a Gen instance.
Block form: (take-while (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — PredicateReturns: GEN — Generator
Examples:
(((Gen count-from 1) take-while (fn (_ x) (< x 4))) ->list) => (1 2 3)(drop-while p)Skip the leading run for which p holds, then yield the rest (lazy).
Instance method: called on a Gen instance.
Block form: (drop-while (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — PredicateReturns: GEN — Generator
Examples:
(((Gen range 0 6) drop-while (fn (_ x) (< x 3))) ->list) => (3 4 5)(enumerate)Pair each value with its 0-based index: (index . value) (lazy).
Instance method: called on a Gen instance.
Returns: GEN — Generator
Examples:
(((Gen of 10 20) enumerate) ->list) => ((0 . 10) (1 . 20))(zip-with f other)Combine corresponding values of two generators with f; stops at the shorter (lazy).
Instance method: called on a Gen instance.
Block form: (zip-with (a b) body … other).
Parameters:
CALLABLE — Combiner (a b) -> cGEN — Other generatorReturns: GEN — Generator
Examples:
(((Gen range 0 3) zip-with (fn (_ a b) (+ a b)) (Gen range 10 13)) ->list) => (10 12 14)(zip other)Pair corresponding values of two generators; stops at the shorter (lazy).
Instance method: called on a Gen instance.
Parameters:
GEN — Other generatorReturns: GEN — Generator
Examples:
(((Gen range 0 2) zip (Gen of 10 20)) ->list) => ((0 . 10) (1 . 20))(scan f init)Running left-fold: emits init’s successive updates (lazy).
Instance method: called on a Gen instance.
Block form: (scan (acc x) body … init) – or (acc i x) with the 0-based index ahead of the element.
Parameters:
CALLABLE — (acc value) -> accANY — Initial accumulatorReturns: GEN — Generator
Examples:
(((Gen range 1 5) scan (fn (_ a x) (+ a x)) 0) ->list) => (1 3 6 10)(fold f acc)Left-fold f over every value.
Instance method: called on a Gen instance.
Block form: (fold (acc x) body … acc) – or (acc i x) with the 0-based index ahead of the element.
Parameters:
CALLABLE — (acc value) -> accANY — Initial accumulatorReturns: ANY — Final accumulator
Examples:
((Gen range 1 5) fold (fn (_ a x) (+ a x)) 0) => 10(for-each f)Apply f to each value, in order.
Instance method: called on a Gen instance.
Block form: (for-each (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — Applied for side effectsReturns: GEN — self
(->list)Materialise the generator as a list.
Instance method: called on a Gen instance.
Returns: LIST — All the values
(->vector)Materialise the generator as a vector.
Instance method: called on a Gen instance.
Returns: VECTOR — All the values
(count)Count the values by consuming the generator (not for infinite ones). Deliberately count, not length: a lazy stream has no length property – counting it is an action.
Instance method: called on a Gen instance.
Returns: INT — Count
(sum)Sum of the values.
Instance method: called on a Gen instance.
Returns: NUMBER — Sum
(product)Product of the values.
Instance method: called on a Gen instance.
Returns: NUMBER — Product
(empty?)Test whether the generator yields no values. Peeks one step – a Gen is persistent, so nothing is consumed.
Instance method: called on a Gen instance.
Returns: BOOL — #t when empty
(reduce f)Fold using the first value as the seed; errors on an empty generator (empty? is the presence door).
Instance method: called on a Gen instance.
Block form: (reduce (a b) body …).
Parameters:
CALLABLE — (acc value) -> accReturns: ANY — Reduced value
(any? p)t if p holds for any value; short-circuits.
Instance method: called on a Gen instance.
Block form: (any? (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — PredicateReturns: BOOL — t/f
(all? p)t if p holds for every value; short-circuits.
Instance method: called on a Gen instance.
Block form: (all? (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — PredicateReturns: BOOL — t/f
(none? p)t if p holds for no value.
Instance method: called on a Gen instance.
Block form: (none? (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — PredicateReturns: BOOL — t/f
(find p)The first value satisfying p, or ().
Instance method: called on a Gen instance.
Block form: (find (x) body …) – or (i x) for the 0-based index, then the element.
Parameters:
CALLABLE — PredicateReturns: ANY — Value or nil
(ref n)The n-th value (0-based); errors when n is negative (a lazy stream has no end to count from) or past the last value.
Instance method: called on a Gen instance.
Parameters:
INT — 0-based indexReturns: ANY — Value at n
(first)The first value; errors on an empty generator (empty? is the presence door).
Instance method: called on a Gen instance.
Returns: ANY — First value
(last)The last value; errors on an empty generator (drives the whole generator).
Instance method: called on a Gen instance.
Returns: ANY — Last value
(min)The least value; errors on an empty generator (drives the whole generator).
Instance method: called on a Gen instance.
Returns: ANY — Least value
(max)The greatest value; errors on an empty generator (drives the whole generator).
Instance method: called on a Gen instance.
Returns: ANY — Greatest value