An abstraction for a stateful reduce operation.
A helper object to reduce collections using an associative operation and to produce stateful reducer objects.
A helper object to reduce collections using an associative operation and to produce stateful reducer objects.
Instead of aggregating from the left to the right, the result will be aggregated from the bottom to the top. E.g. for a sequence Array(1,2,3,4) and an operation (+), the reducers produced by this helper class would execute (1 + 2) + (3 + 4) instead of (((1+2)+3)+4). This can have significant advantages when the cost of an operation depends on the weight of an element.
Consider string concatenation: concatenating a sequence of one-char strings of size N using seq.reduceLeft(_ + _) would be an O(N**2) operation. Reducing it hierarchically would be an O(N*log(N)) operation.
An abstraction for a stateful reduce operation. The operation is called once for each element of the sequence to be reduced, and then the result (if any) is retrieved using the result() method. Note that the result() operation is side-effecting, and the reducer should no longer be used after calling result(). This is similar to a typical builder.
the element and result type