Paccum : Pattern
An accumulator -- repeatedly performs a binary operation on the previous stream value and the result of a second stream. Each output value becomes the input value for the next calculation.
Where Pseries adds a constant value on successive iterations, here the second operand is variable.
*new(lo = -inf, hi = inf, step, length = inf, start, operator = '+')
lo = minimum output value; values lower than this are .fold-ed back in range
hi = maximum output value
step = pattern for the second operand
length = number of values to output
start = starting value; if not given, the stream will start with rrand(lo, hi)
operator = by default, '+'; you may use other operators. This may be a pattern to do different operations per iteration.
Examples:
p = Pseries(0, 1, inf).asStream;
p.nextN(10);
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
p = Paccum(step: 1, length: inf, start: 0).asStream;
p.nextN(10);
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
p = Paccum(step: Pwhite(1, 5, inf), length: inf, start: 0).asStream;
p.nextN(10);
[ 0, 4, 8, 10, 11, 14, 17, 18, 23, 25 ]
p = Paccum(step: Pwhite(1, 5, inf), length: inf, start: 200, operator: '/').asStream;
p.nextN(10);
[ 200, 100, 20, 10, 2, 1, 1, 0.33333333333333, 0.16666666666667, 0.033333333333333 ]