In List§

See primary documentation in context for routine combinations

multi sub    combinations($from$of = 0..*             --> Seq:D)
multi method combinations(List:D: Int() $of             --> Seq:D)
multi method combinations(List:D: Iterable:D $of = 0..* --> Seq:D)

Returns a Seq with all $of-combinations of the invocant list. $of can be a numeric Range, in which case combinations of the range of item numbers it represents will be returned (i.e. 2.6 .. 4 will return 2-, 3-, and 4-item combinations). Otherwise, $of is coerced to an Int.

.say for <a b c>.combinations: 2;
# OUTPUT: 
# (a b) 
# (a c) 
# (b c)

Above, there are three possible ways to combine the 2-items lists from the original list, which is what we receive in the output. See permutations if you want permutations instead of combinations.

With Range argument, we get both three 2-item combinations and one 3-item combination:

.say for <a b c>.combinations: 2..3;
# OUTPUT: 
# (a b) 
# (a c) 
# (b c) 
# (a b c)

If $of is negative or is larger than there are items in the given list, an empty list will be returned. If $of is zero, a 1-item list containing an empty list will be returned (there's exactly 1 way to pick no items).

The subroutine form is equivalent to the method form called on the first argument ($from), with the exception that if $from is not an Iterable, it gets coerced to an Int and combinations are made from a Range constructed with 0..^$from instead:

.say for combinations 32
# OUTPUT: 
# (0 1) 
# (0 2) 
# (1 2)

Note: some implementations may limit the maximum value of non-Iterable $from. On Rakudo, 64-bit systems have a limit of 2³¹-1 and 32-bit systems have a limit of 2²⁸-1.

In Any§

See primary documentation in context for method combinations

method combinations(|c)

Coerces the invocant to a list by applying its .list method and uses List.combinations on it.

say (^3).combinations# OUTPUT: «(() (0) (1) (2) (0 1) (0 2) (1 2) (0 1 2))␤» 

Combinations on an empty data structure will return a list with a single element, an empty list; on a data structure with a single element it will return a list with two lists, one of them empty and the other with a single element.

say set().combinations# OUTPUT: «(())␤»