permutations
ErrorsCollection

permutations

Synthesised documentation from type/Any type/List

From type/Any

See Original text in context

method permutations(|c)

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

say <a b c>.permutations;
# OUTPUT: «((a b c) (a c b) (b a c) (b c a) (c a b) (c b a))␤» 
say set(1,2).permutations;
# OUTPUT: «((2 => True 1 => True) (1 => True 2 => True))␤»

Permutations of data structures with a single or no element will return a list containing an empty list or a list with a single element.

say 1.permutations# OUTPUT: «((1))␤»

From type/List

See Original text in context

multi sub    permutations(Int()    $from --> Seq:D)
multi sub    permutations(Iterable $from --> Seq:D)
multi method permutations(List:D:        --> Seq:D)

Returns all possible permutations of a list as a Seq of lists:

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

permutations treats all elements as unique, thus (1, 1, 2).permutations returns a list of 6 elements, even though there are only three distinct permutations, due to first two elements being the same.

The subroutine form behaves the same as the method form, computing permutations from its first argument $from. If $from is not an Iterable, coerces $from to an Int and picks from a Range constructed with 0..^$from:

.say for permutations 3;
# OUTPUT: 
# (0 1 2) 
# (0 2 1) 
# (1 0 2) 
# (1 2 0) 
# (2 0 1) 
# (2 1 0)