runningFoldIndexed<R> method

Iterable<R> runningFoldIndexed<R>(
  1. R initialValue,
  2. R combine(
    1. int index,
    2. R previousValue,
    3. E element
    )
)

Returns a Iterable containing successive accumulation values generated by applying combine function from left to right to each element and current accumulator value that starts with initialValue.

Implementation

Iterable<R> runningFoldIndexed<R>(
  R initialValue,
  R Function(int index, R previousValue, E element) combine,
) {
  var previousValue = initialValue;

  return [initialValue].followedBy(withIndex.map((indexedValue) {
    previousValue = combine(
      indexedValue.index,
      previousValue,
      indexedValue.value,
    );

    return previousValue;
  }));
}