scan<E> method
Returns an IList containing successive accumulation values generated by applying
combine from left to right to each element and the current accumulator value,
starting with initialValue.
Similar to fold, but instead of returning only the final accumulated value, it returns all intermediate accumulator values as a list.
The result always starts with initialValue and has length this.length + 1.
Example:
[1, 2, 3].lock.fold(0, (acc, e) => acc + e); // Returns: 6
[1, 2, 3].lock.scan(0, (acc, e) => acc + e); // Returns: [0, 1, 3, 6]
The accumulator type E can differ from the element type T:
[1, 2, 3].lock.scan<String>('', (acc, e) => '$acc$e'); // Returns: ['', '1', '12', '123']
Implementation
IList<E> scan<E>(E initialValue, E Function(E previousValue, T element) combine) {
final result = <E>[initialValue];
E accumulator = initialValue;
for (final element in this) {
accumulator = combine(accumulator, element);
result.add(accumulator);
}
return IList._unsafe(LFlat<E>.unsafe(result), config: config);
}