reduceIndexed method

T reduceIndexed(
  1. T combine(
    1. int index,
    2. T previous,
    3. T element
    )
)

Combine the elements with each other and the current index.

Calls combine for each element except the first. The call passes the index of the current element, the result of the previous call, or the first element for the first call, and the current element.

Returns the result of the last call, or the first element if there is only one element. There must be at least one element.

Implementation

T reduceIndexed(T Function(int index, T previous, T element) combine) {
  var iterator = this.iterator;
  if (!iterator.moveNext()) {
    throw StateError('no elements');
  }
  var index = 1;
  var result = iterator.current;
  while (iterator.moveNext()) {
    result = combine(index++, result, iterator.current);
  }
  return result;
}