Permalink
Browse files

first attempt

  • Loading branch information...
1 parent f6e11ba commit 5a7979ee1860e3dc9fbb3cb4d9dcf99a44517842 @grondilu grondilu committed Oct 2, 2013
Showing with 27 additions and 0 deletions.
  1. +27 −0 src/core/List.pm
View
@@ -532,6 +532,33 @@ my class List does Positional { # declared in BOOTSTRAP
method STORE_AT_POS(Int \pos, Mu \v) is rw {
nqp::bindpos($!items, nqp::unbox_i(pos), v)
}
+
+ my sub combinations(Int $n, Int $k) {
+ return [] if $k == 0;
+ return () if $k > $n;
+ gather {
+ take [0, (1..^$n)[@$_]] for combinations($n-1, $k-1);
+ take [(1..^$n)[@$_]] for combinations($n-1, $k );
+ }
+ }
+ proto method combinations($) {*}
+ multi method combinations( Int $of ) {
+ gather take self[@$_] for combinations self.elems, $of
+ }
+ multi method combinations( Range $of = 0 .. * ) {
+ X::NYI.new.throw;
+ }
+
+ my sub permutations(Int $n) {
+ $n == 1 ?? ( [0,] ) !!
+ gather for ^$n -> $i {
+ my @i = grep none($i), ^$n;
+ take [$i, @i[@$_]] for permutations($n - 1);
+ }
+ }
+ method permutations() {
+ gather take self[@$_] for permutations self.elems;
+ }
}
sub eager(|) {

0 comments on commit 5a7979e

Please sign in to comment.