reduce

Traverse items of a collection while applying a lambda allowing data to be combined in a particular way to return a result.

reduce can be applied on a map< K, V >, list< T >, or set< T >.

Parameters:

No. Type Comments
1 map< K, V >, list< T > or set< T > The object on which the reduce should be applied
2 Initial value of type R An initial value for the accumulator
3 A lambda that accepts 2 or 3 parameter The 1st lambda parameter will always be the current accumulator.
When 1st parameter to reduce is a list< T > or set< T >,then the 2nd parameter to lambda is one of its elements.
When 1st parameter to map is a map< K, V >, the lambda accepts 3 parameters - accumulator, entry key and entry value.

Returns:

The final value of the accumulator, which was the 2nd argument to reduce.

Usages:

  • reduce( v:list<T>, accumulator : R, ( acc, e ) => result : R ) : R
  • reduce( v:set<T>, accumulator : R,  ( acc, e ) => result : R ) : R
  • reduce( v:map<K, V>, accumulator : R, (acc, k, v) => result : R ) : R