routine permutations
1 | class List |
1.1 | (List) routine permutations |
2 | class Any |
2.1 | (Any) method permutations |
Documentation for routine permutations
assembled from the following types:
class List
From List
(List) routine permutations
Defined as:
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
. 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)
class Any
From Any
(Any) method permutations
Defined as:
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))»