runningReduceIndexed<S> method

  1. @useResult
KtList<S> runningReduceIndexed<S>(
  1. S operation(
    1. int index,
    2. S acc,
    3. T
    )
)

Returns a list containing successive accumulation values generated by applying operation from left to right to each element, its index in the original collection and current accumulator value that starts with the first element of this collection.

Implementation

@useResult
KtList<S> runningReduceIndexed<S>(S Function(int index, S acc, T) operation) {
  final i = iterator();
  if (!i.hasNext()) {
    return emptyList();
  }
  S accumulator = i.next() as S;
  final result = mutableListOf<S>()..add(accumulator);
  int index = 1;
  while (i.hasNext()) {
    accumulator = operation(index++, accumulator, i.next());
    result.add(accumulator);
  }
  return result;
}