Skip to content

Instantly share code, notes, and snippets.

@ylegall
Created October 17, 2013 20:25
Show Gist options
  • Save ylegall/7031638 to your computer and use it in GitHub Desktop.
Save ylegall/7031638 to your computer and use it in GitHub Desktop.
combinations
import std.stdio;
import std.algorithm;
void combinations(T)(T[] array) {
comb(array, []);
}
private void comb(T)(T[] array, T[] prefix) {
if (array.length) {
writeln(prefix ~ [array[0]]);
comb(array[1..$], prefix ~ [array[0]]);
comb(array[1..$], prefix);
}
}
void main() {
combinations([1,2,3,4]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment