Skip to content

Commit

Permalink
first attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
grondilu committed Oct 2, 2013
1 parent f6e11ba commit 5a7979e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/core/List.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -532,6 +532,33 @@ my class List does Positional { # declared in BOOTSTRAP
method STORE_AT_POS(Int \pos, Mu \v) is rw { method STORE_AT_POS(Int \pos, Mu \v) is rw {
nqp::bindpos($!items, nqp::unbox_i(pos), v) 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(|) { sub eager(|) {
Expand Down

0 comments on commit 5a7979e

Please sign in to comment.