foldWithNext<V> method

V foldWithNext<V>(
  1. V initialValue,
  2. V combiner(
    1. V previousValue,
    2. E current,
    3. E next
    )
)

Reduces a collection to a single value by iteratively combining previous and current element of the collection with an existing value

Implementation

V foldWithNext<V>(V initialValue, V Function(V previousValue, E current, E next) combiner) {
  Iterator<E> iterator = this.iterator;
  if (!iterator.moveNext()) {
    throw StateError("No element");
  }
  var value = initialValue;
  E current = iterator.current;
  while (iterator.moveNext()) {
    value = combiner(value, current, iterator.current);
    current = iterator.current;
  }
  return value;
}