innerProduct method

T innerProduct(
  1. Iterable<T> other,
  2. T init, {
  3. T op1(
    1. T,
    2. T
    )?,
  4. T op2(
    1. T,
    2. T
    )?,
})

Computes the inner product (dot product) of two ranges.

Similar to std::inner_product. The default sums the products of corresponding elements. op1 accumulates the results (default +), and op2 computes the product (default *).

Implementation

T innerProduct(
  Iterable<T> other,
  T init, {
  T Function(T, T)? op1,
  T Function(T, T)? op2,
}) {
  op1 ??= (T a, T b) => ((a as dynamic) + (b as dynamic)) as T;
  op2 ??= (T a, T b) => ((a as dynamic) * (b as dynamic)) as T;

  var it1 = iterator;
  var it2 = other.iterator;
  T result = init;
  while (it1.moveNext() && it2.moveNext()) {
    result = op1(result, op2(it1.current, it2.current));
  }
  return result;
}