accum - compute the incremental result of an operation
arr.accum(
op)
arr.accum(
op,
axis)
Similar to APL op \
arr
and op \[axis]
arr
. Like .reduce()
,
.accum()
inserts the given operator op
between elements of the array. However, the number of elements used
increases across the axis as the operation proceeds. The first
element along an axis of the result is simply the original array
element. The next element along an axis of the result is the operator
applied to the first two elements. The third element along an axis of
the result is the operator applied to the first three elements. And
so on. Like .reduce()
(and like APL) each result element operation is evaluated
right-to-left.
If op is a Proc
then it is called with
sequential elements of the array and is expected to return a numeric
result.
The axis is an axis specification and specifies which axis the reduction should be performed along.
A new array which contains the result of the accumulation operator
TypeCheck
if arr is not a scalar or
array
ArgCheck
if an incorrect number of
arguments is given
RangeCheck
if
axis is not a valid axis of arr
arr = 5->iterate(); arr.accum(`-) 0 -1 1 -2 2 proc sub(a,b) { "(", a, " - ", b, ") = ", a - b, '\n'; return a - b; } arr.accum(sub) (3 - 4) = -1 (2 - -1) = 3 (1 - 3) = -2 (0 - -2) = 2 (2 - 3) = -1 (1 - -1) = 2 (0 - 2) = -2 (1 - 2) = -1 (0 - -1) = 1 (0 - 1) = -1 0 -1 1 -2 2 a = [3,4].iterate() a.accum(`+) 0 1 3 6 4 9 15 22 8 17 27 38 a.accum(`+,0) 0 1 2 3 4 6 8 10 12 15 18 21