combinations method

Iterable<List<E>> combinations(
  1. int count, {
  2. bool repetitions = false,
})

Returns an iterable over the combinations of this Iterable of length count. The combinations are emitted in lexicographical order based on the input.

If repetitions is set to true the iterable allows individual elements to be repeated more than once. The number of items returned is:

(elements.length + count - 1)! / count! / (elements.length - 1)!

The following expression iterates over xx, xy, xz, yy, yz, and zz:

['x', 'y', 'z'].combinations(2, repetitions: true);

If repetitions is set to false the iterable generates all the sub-sequences of length count. The number of items returned is:

elements.length! / count! / (elements.length - count)!

The following expression iterates over xy, xz, yz:

['x', 'y', 'z'].combinations(2, repetitions: false);

Implementation

Iterable<List<E>> combinations(int count, {bool repetitions = false}) {
  RangeError.checkNotNegative(count, 'count');
  final list = toList(growable: false);
  if (!repetitions && list.length < count) {
    throw RangeError.range(count, 0, list.length, 'count');
  } else if (repetitions) {
    return combinationsWithRepetitions<E>(list, count);
  } else {
    return combinationsWithoutRepetitions<E>(list, count);
  }
}