reduceIndexed<S> method

S reduceIndexed<S>(
  1. S operation(
    1. int index,
    2. S acc,
    3. T
    )
)

Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element with its index in the original collection. @param operation function that takes the index of an element, current accumulator value and the element itself and calculates the next accumulator value.

Implementation

S reduceIndexed<S>(S Function(int index, S acc, T) operation) {
  final i = iterator();
  if (!i.hasNext()) {
    throw UnsupportedError("Empty collection can't be reduced.");
  }
  var index = 1;
  S accumulator = i.next() as S;
  while (i.hasNext()) {
    accumulator = operation(index++, accumulator, i.next());
  }
  return accumulator;
}