Reduce
Combines the elements of an array into one value.
Node id array/reduce
The Reduce node goes through array one element at a time, keeping a
running value (the accumulator): it starts from initial, each call of
function gives the next one, and the last one is the result. Summing
numbers, or finding the largest shape, are reductions.
The function is a subgraph. To make one that fits,
drag a wire from Reduce's
function input onto empty canvas and choose
New function…, or right-click Reduce and choose New function….Inputs
| Name | Type | Default | Notes |
|---|---|---|---|
| array | Array<T> | — | generic |
| initial | A | — | generic |
| function | Function(A, T, number, ...) -> (A) | — | generic |
| arguments | X | — | multigenericindependent |
Outputs
| Name | Type | Notes |
|---|---|---|
| result | A | generic |
Behaviour
- The function has the type
Function(A, T, number, ...) -> (A): it gets the accumulator, the element and its index, and gives the new accumulator. See Functions. - The accumulator's type comes from
initialand the function, and needn't be the element type: reducing an array of circles can give a number. - Extra arguments. Values connected to
arguments(a multi input) are passed to every call after the index, in slot order, as for Map. - An empty array gives
initial, without calling the function. - Each call is separate, with its own random numbers.
Errors
functionmust be connected.- The function must take exactly as many extra parameters as there are
arguments, each of the right type, checked before any call. - If the function fails for any element, Reduce fails.
Related nodes
- Map: one result per element instead of one in total.
- Repeat: a running value without an array.
- Average, Sum Vectors and Union: ready-made reductions.