Skip to content

Commit

Permalink
Document List.combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Feb 10, 2014
1 parent 57b9dd3 commit 1c5b353
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/List.pod
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,52 @@ Example:
@foo.unshift: 1, 3 ... 11;
say @foo; # 1 3 5 7 9 11 a b c
=head2 combinations
multi method combinations (List:D: Int:D $of) returns List:D
multi method combinations (List:D: Range:D $of = 0..*) returns List:D
multi sub combinations ($n, $k) returns List:D
The C<Int> variant teturns all C<$of>-combinations of the invocant list.
For example
say .join('|') for <a b c>.combinations(2);
prints
a|b
a|c
b|c
because all the 2-combinations of C<'a', 'b', 'c'> are
C<['a', 'b'], ['a', 'c'], ['b', 'c']>.
The C<Range> variant combines all the individual combinations into a single
list, so
say .join('|') for <a b c>.combinations(2..3);
prints
a|b
a|c
b|c
a|b|c
because that's the list of all 2- and 3-combinations.
The subroutine form C<combinations($n, $k)> is equivalent to
C<(^$n).combinations($k)>, so
.say for combinations(4, 2)
prints
0 1
0 2
0 3
1 2
1 3
2 3
=end pod

0 comments on commit 1c5b353

Please sign in to comment.