Documentation for routine combinations
Documentation for routine combinations, assembled from the following types:
method combinations(--> Seq)
Any as a 1-item list and uses
List.combinations on it.
say Any.combinations; # OUTPUT: «(() ((Any)))␤»
multi sub combinations(, --> Seq)multi method combinations(List: Int --> Seq)multi method combinations(List: Range = 0..* --> Seq)
Int variant returns all
$of-combinations of the invocant list. For example
say .join('|') for <a b c>.combinations(2);# OUTPUT: «a|b␤# a|c␤# b|c␤»
because all the 2-combinations of
'a', 'b', 'c' are
['a', 'b'], ['a', 'c'], ['b', 'c'].
Range variant combines all the individual combinations into a single list, so
say .join('|') for <a b c>.combinations(2..3);# OUTPUT: «a|b# a|c␤# b|c␤# a|b|c␤»
because that's the list of all 2- and 3-combinations.
The subroutine form
combinations($n, $k) is equivalent to
.say for combinations(4, 2)# OUTPUT: «0 1# 0 2␤# 0 3␤# 1 2␤# 1 3␤# 2 3␤»
$k is negative or is larger than there are items in the given list, an empty list will be returned. If
$k is zero, a 1-item list containing an empty list will be returned (there's exactly 1 way to pick no items).
Note: some implementations may limit the maximum
$n. On Rakudo, 64-bit systems have a limit of
2³¹-1 and 32-bit systems have a limit of