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');
  final list = <List<E>>[];
  for (final iterable in this) {
    final subList = iterable.toList(growable: false);
    if (subList.isEmpty) return const [];
    list.add(subList);
  }
  if (list.isEmpty) return const [];
  return productNotEmpty(list, repeat);
}