cross3With<A, B, C, R> function

List<R> cross3With<A, B, C, R>(
  1. Iterable<A> a,
  2. Iterable<B> b,
  3. Iterable<C> c,
  4. R reduce(
    1. A a,
    2. B b,
    3. C c
    ),
)

Returns the 3-way Cartesian product of a, b, and c mapped through reduce.

Implementation

List<R> cross3With<A, B, C, R>(
  Iterable<A> a,
  Iterable<B> b,
  Iterable<C> c,
  R Function(A a, B b, C c) reduce,
) {
  final listA = a is List<A> ? a : List<A>.of(a, growable: false);
  final listB = b is List<B> ? b : List<B>.of(b, growable: false);
  final listC = c is List<C> ? c : List<C>.of(c, growable: false);
  final na = listA.length;
  final nb = listB.length;
  final nc = listC.length;
  if (na == 0 || nb == 0 || nc == 0) return <R>[];
  final out = <R>[];
  for (var i = 0; i < na; ++i) {
    final va = listA[i];
    for (var j = 0; j < nb; ++j) {
      final vb = listB[j];
      for (var k = 0; k < nc; ++k) {
        out.add(reduce(va, vb, listC[k]));
      }
    }
  }
  return out;
}