sum<R extends num> method
Computes the sum of all values. Values are created from this iterable's elements using select
.
The initial value is 0 if unspecified. Returns double.nan if select
returns double.nan.
Example
final list = [('a', 2), ('b', 3), ('c', 4)];
list.sum((e) => e.$2, initial: 1); // 10
Implementation details
Computing values is assumed to be cheap. Hence, values are recomputed each time rather than cached.
Implementation
@useResult R sum<R extends num>(Select<E, num> select, {R? initial}) {
var sum = initial ?? 0;
for (final element in this) {
sum += select(element);
}
// Dart is fucking stupid for not allowing implicit conversions between integers and doubles.
return (R == int ? sum.toInt() : sum.toDouble()) as R;
}