permutations method

Iterable<List<E>> permutations([
  1. int? count
])

Returns an iterable over the permutations of this Iterable of length count. If no count is specified all full-length permutations are generated. The permutations are emitted in lexicographical order based on the input.

The following expression iterates over xyz, xzy, yxz, yzx, zxy, and zyx:

['x', 'y', 'z'].permutations();

The following expression iterates over the permutations of length 2, these are: xy, xz, yx, yz, zx, and zy:

['x', 'y', 'z'].permutations(2);

Implementation

Iterable<List<E>> permutations([int? count]) {
  final elements = toList(growable: false);
  count ??= elements.length;
  if (count == 0 || elements.isEmpty) {
    return const [];
  } else if (count == elements.length) {
    return _fullPermutations(elements);
  } else if (0 < count && count < elements.length) {
    return _partialPermutations(elements, count);
  }
  throw RangeError.range(count, 0, elements.length, 'count');
}