Created
October 2, 2013 16:24
-
-
Save grondilu/6796390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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