Methods by type
Combinations & permutations
FullGenerate every k-subset or every ordering of a list.
.combinations yields every subset (order doesn't matter); .permutations yields every ordering (order does). Both are lazy and produced in a deterministic order.
combinations #
.combinations(k) returns every k-element subset, in order.
say (1, 2, 3).combinations(2);
Output
((1 2) (1 3) (2 3))With no argument it returns subsets of every size, from empty to the whole list.
permutations #
.permutations returns every ordering — n! of them for n elements.
say <a b c>.permutations.elems; say <a b c>.permutations.map(*.join).sort;
Output
6
(abc acb bac bca cab cba)Notes #
- Both are lazy
Seqs, so.permutationsof a large list is fine to take from with[^k]without generating alln!up front. - Each result is a sublist, so
sayshows them nested —((1 2) (1 3) (2 3)). - For random selection instead of exhaustive generation, use
.pick(without replacement) or.roll(with replacement).