foldIndexed<S> method
Null safety
- S initial,
- IndexedAccumulate<
S, E> f
Accumulates a collection to a single value of type S
, which starts from an initial
value,
by combining each element with the current accumulator value,
with the combinator f
, providing sequential index of the element.
Example:
[1, 2, 5, 8].foldIndexed(0, (i, acc, x) => acc + (i % 2 * x)) // => 0 + 0 + 2 + 0 + 8 => 10
Implementation
S foldIndexed<S>(S initial, IndexedAccumulate<S, E> f) {
var i = 0;
return this?.fold<S>(initial, (p, e) => f(i++, p, e)) ?? initial;
}