Skip to content

Instantly share code, notes, and snippets.

@grondilu
Created October 2, 2013 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grondilu/6796390 to your computer and use it in GitHub Desktop.
Save grondilu/6796390 to your computer and use it in GitHub Desktop.
use MONKEY_TYPING;
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 );
}
}
augment class List {
method combinations(Int $n) {
gather take self[@$_] for combinations self.elems, $n
}
}
say (^6).list.combinations(3).perl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment