reduce<T, R> function

R reduce<T, R>(
  1. Iterable<T> values,
  2. R reducer(
    1. R accumulator,
    2. T value,
    3. int index,
    4. Iterable<T> values,
    ),
  3. R initialValue
)

Applies reducer against an accumulator initialized to initialValue and each element of values to reduce it to a single value of type R.

Implementation

// DIVERGES(d3-array): adapt-array-typed-api, split typed reduce (with
// initialValue) and reduceFirst (without initialValue, returning T?).
R reduce<T, R>(
  Iterable<T> values,
  R Function(R accumulator, T value, int index, Iterable<T> values) reducer,
  R initialValue,
) {
  var value = initialValue;
  var index = -1;
  for (final next in values) {
    value = reducer(value, next, ++index, values);
  }
  return value;
}