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

12 lines
299 B
D
Raw Permalink Normal View History

2013-04-10 16:57:12 -07:00
import std.stdio, std.algorithm, std.range;
2015-02-20 00:35:01 -05:00
immutable(int)[][] comb(immutable int[] s, in int m) pure nothrow @safe {
2013-04-10 16:57:12 -07:00
if (!m) return [[]];
if (s.empty) return [];
2015-02-20 00:35:01 -05:00
return s[1 .. $].comb(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].comb(m);
2013-04-10 16:57:12 -07:00
}
void main() {
2014-01-17 05:32:22 +00:00
4.iota.array.comb(2).writeln;
2013-04-10 16:57:12 -07:00
}