The reduce function follows the conventions of Array#reduce.
If the iterator is empty, an initial value is required. That value
will be used as the return value. If no initial value is provided,
an error will be thrown.
If the iterator contains a single item and no initial value is
provided, the single item is used as the return value.
Otherwise, the reducer is invoked for each element in the iterable.
If an initial value is not provided, the first element will be used
as the initial accumulated value.
Summarize all values in an iterable using a reducer function.
Returns
The final accumulated value.
Notes
The
reducefunction follows the conventions ofArray#reduce.If the iterator is empty, an initial value is required. That value will be used as the return value. If no initial value is provided, an error will be thrown.
If the iterator contains a single item and no initial value is provided, the single item is used as the return value.
Otherwise, the reducer is invoked for each element in the iterable. If an initial value is not provided, the first element will be used as the initial accumulated value.
Complexity
Linear.
Example