sum method

num sum([
  1. num addend(
    1. E
    )?
])

Returns the sum of all the values in this iterable.

If addend is provided, it will be used to compute the value to be summed.

Returns 0 if this is empty.

Example:

[1, 2, 3].sum(); // 6.
[2, 3, 4].sum((i) => i * 0.5); // 4.5.
[].sum() // 0.

Implementation

num sum([num Function(E)? addend]) {
  if (this.isEmpty) return 0;
  return addend == null
      ? this.reduce((a, b) => (a + b) as E)
      : this.fold(0, (prev, element) => prev + addend(element));
}