product method

Iterable<List<E>> product({
  1. int repeat = 1,
})

Returns an iterable over the cross product of this Iterable.

The resulting iterable is equivalent to nested for-loops. The rightmost elements advance on every iteration. This pattern creates a lexicographic ordering so that if the input’s iterables are sorted, the product is sorted as well.

For example:

final a = ['x', 'y'];
final b = [1, 2, 3];
print([a, b].product());  // [['x', 1], ['x', 2], ['x', 3],
                          //  ['y', 1], ['y', 2], ['y', 3]]

Implementation

Iterable<List<E>> product({int repeat = 1}) {
  checkNonZeroPositive(repeat, 'repeat');
  if (isEmpty || any((iterable) => iterable.isEmpty)) {
    return const [];
  } else {
    return productNotEmpty(
        map((iterable) => iterable.toList(growable: false))
            .toList(growable: false),
        repeat);
  }
}