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.
[1, 2, 3].sum((i) => i * 3); // 18.
[].sum() // 0.

Implementation

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