sum<T> static method

num sum<T>(
  1. Iterable<T> list, {
  2. num by(
    1. T
    )?,
})

Returns the sum of list. Pass by to extract a number from each element; otherwise list must be Iterable<num>.

Arr.sum([1, 2, 3]);                                  // 6
Arr.sum(orders, by: (o) => o.total);                  // sum of totals

Implementation

static num sum<T>(Iterable<T> list, {num Function(T)? by}) {
  var total = 0.0;
  for (final v in list) {
    total += by != null ? by(v) : (v as num);
  }
  return total == total.toInt() ? total.toInt() : total;
}