runningReduce<S> method

KtList<S> runningReduce<S>(
  1. S operation(
    1. S acc,
    2. T
    )
)

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

Implementation

KtList<S> runningReduce<S>(S Function(S acc, T) operation) {
  final i = iterator();
  if (!i.hasNext()) {
    return emptyList();
  }
  S accumulator = i.next() as S;
  final result = mutableListOf<S>()..add(accumulator);
  while (i.hasNext()) {
    accumulator = operation(accumulator, i.next());
    result.add(accumulator);
  }
  return result;
}