fold<U> method

  1. @override
U fold<U>(
  1. U initialValue,
  2. U f(
    1. U previousValue,
    2. T element
    )
)
override

Reduces a collection to a single value by iteratively combining each element of the collection with an existing value

Uses initialValue as the initial value, then iterates through the elements and updates the value with each element using the combine function, as if by:

var value = initialValue;
for (E element in this) {
  value = combine(value, element);
}
return value;

Example of calculating the sum of an iterable:

final numbers = <double>[10, 2, 5, 0.5];
const initialValue = 100.0;
final result = numbers.fold<double>(
    initialValue, (previousValue, element) => previousValue + element);
print(result); // 117.5

Implementation

@override
@pragma("vm:prefer-inline")
U fold<U>(U initialValue, U Function(U previousValue, T element) f) =>
    _list.getRange(_start, _end).fold(initialValue, f);