RosettaCodeData/Task/Combinations/D/combinations-1.d

14 lines
333 B
D
Raw Permalink Normal View History

2013-04-10 16:57:12 -07:00
T[][] comb(T)(in T[] arr, in int k) pure nothrow {
if (k == 0) return [[]];
typeof(return) result;
2014-01-17 05:32:22 +00:00
foreach (immutable i, immutable x; arr)
2013-04-10 16:57:12 -07:00
foreach (suffix; arr[i + 1 .. $].comb(k - 1))
result ~= x ~ suffix;
return result;
}
void main() {
2014-01-17 05:32:22 +00:00
import std.stdio;
[0, 1, 2, 3].comb(2).writeln;
2013-04-10 16:57:12 -07:00
}