sum method

dynamic sum([
  1. dynamic selector(
    1. E
    )?
])

Returns the sum of all elements in this iterable.

For numeric types, this adds all elements together. Example: [1, 2, 3].sum() => 6

For non-numeric types, you must provide a selector function to extract a numeric value from each element. Example: ['a', 'aa', 'aaa'].sum((s) => s.length) => 6

If any element is a Complex number, all elements will be converted to Complex and the result will be Complex. Example: [1, 2, Complex(1,5), 9.7] => Complex(12.7, 5)

Implementation

dynamic sum([Function(E)? selector]) {
  if (selector != null) {
    return fold<dynamic>(0, (sum, element) => sum + selector(element));
  }

  try {
    // Convert all elements to Complex and sum them
    return fold<Complex>(Complex.zero(), (sum, element) {
      final complexElement = element is Complex ? element : Complex(element);
      return sum + complexElement;
    }).simplify();
  } catch (e) {
    throw ArgumentError(
        'Cannot sum non-numeric elements without a selector function');
  }
}