accumulate method

T accumulate(
  1. T init, [
  2. T op(
    1. T,
    2. T
    )?
])

Sums up or folds the elements in the range, starting with init.

Similar to std::accumulate. The default operation is addition, but a custom op can be provided. Note that if no custom op is provided, T must support the + operator.

Implementation

T accumulate(T init, [T Function(T, T)? op]) {
  op ??= (T a, T b) => ((a as dynamic) + (b as dynamic)) as T;
  return fold(init, op);
}