matrix reduce for filters
Reduce all elements of a matrix to a single value.
Signature
> matrix reduce {flags} (closure)
Flags
--fold, -f {any}: The initial value for the accumulator
Parameters
closure: Reducing function. Arguments are the current element, then the accumulator (same order asreduce).
Input/output types:
| input | output |
|---|---|
| matrix | any |
Examples
Sum all elements of a 2x2 matrix
> [[1 2] [3 4]] | into matrix | matrix reduce --fold 0.0 {|e, acc| $acc + $e}
10.0Product of all elements in a matrix
> [[1 2] [3 4]] | into matrix | matrix reduce --fold 1.0 {|e, acc| $acc * $e}
24.0Sum without an initial value (uses first element as starting accumulator)
> [[1 2] [3 4]] | into matrix | matrix reduce {|e, acc| $acc + $e}
10.0