Documentation for routine
permutations assembled from the following types:
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))␤»
multi sub permutations(Int() --> Seq)multi sub permutations(Iterable --> Seq)multi method permutations(List: --> Seq)
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 is not an Iterable, coerces
$from to an
Int and picks from a Range constructed with
.say for permutations 3;# OUTPUT:# (0 1 2)# (0 2 1)# (1 0 2)# (1 2 0)# (2 0 1)# (2 1 0)