NAME

reduce - reduce an array along an axis using an operator

SYNOPSIS

arr.reduce(op)
arr.reduce(op, axis)

DESCRIPTION

The reduce() method reduces the given arr along the specified axis using the operator or procedure op. This is similar to APL op / arr and op /[axis] arr.

The reduction is performed by applying op cumulatively across the elements of the specified axis. For example, a reduction with `+ computes the sum of the elements.

If axis is not provided, it defaults to the last dimension (-1).

If arr is a scalar numeric value, the result is the value itself.

Note that the reduction is implicitly performed right-to-left, which matters if the op is not associative (for example, subtraction).

RETURN VALUE

A new array with one fewer dimension than arr. If arr is a single-dimensional array (vector), the result is a scalar.

ERRORS

ArgCheck if the number of arguments is incorrect
TypeCheck if op is not a valid operator or procedure
RangeCheck if axis is out of the valid range of dimensions

EXAMPLE

    [1:10].reduce(`+)
55

    a = [2, 2].iterate()
    a.reduce(`+, 0)
2 4

    [1:5].reduce(`-)
3

    // Reduction is performed right-to-left...
    1 - (2 - (3 - (4 - 5)))
3

    // ...not left-to-right
    1 - 2 - 3 - 4 - 5
-13

SEE ALSO

accum
nreduce