product method
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, the product of ['x', 'y']
and [1, 2, 3]
is created with
[['x', 'y'], [1, 2, 3]].product();
and results in an iterable with the following elements:
['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);
}
}