cppReduce method

T cppReduce([
  1. T op(
    1. T,
    2. T
    )?
])

Computes a result using an operation over the elements.

Similar to std::reduce. The difference in C++ is that std::reduce can be executed out of order, but here it operates sequentially unless parallelized. It differs from Dart's reduce only by defaulting the op to addition if not provided.

Throws a StateError if the iterable is empty.

Implementation

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