generate
for generators
Generate a list of values by successively invoking a closure.
Signature
> generate {flags} (closure) (initial)
Parameters
closure
: Generator function.initial
: Initial value.
Input/output types:
input | output |
---|---|
nothing | list<any> |
Examples
Generate a sequence of numbers
> generate {|i| if $i <= 10 { {out: $i, next: ($i + 2)} }} 0
╭───┬────╮
│ 0 │ 0 │
│ 1 │ 2 │
│ 2 │ 4 │
│ 3 │ 6 │
│ 4 │ 8 │
│ 5 │ 10 │
╰───┴────╯
Generate a continuous stream of Fibonacci numbers
> generate {|fib| {out: $fib.0, next: [$fib.1, ($fib.0 + $fib.1)]} } [0, 1]
Generate a continuous stream of Fibonacci numbers, using default parameters
> generate {|fib=[0, 1]| {out: $fib.0, next: [$fib.1, ($fib.0 + $fib.1)]} }
Notes
The generator closure accepts a single argument and returns a record containing two optional keys: 'out' and 'next'. Each invocation, the 'out' value, if present, is added to the stream. If a 'next' key is present, it is used as the next argument to the closure, otherwise generation stops.