RosettaCodeData/Task/Partial-function-application/D/partial-function-application.d

20 lines
430 B
D
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
import std.stdio, std.algorithm, std.traits;
2013-10-27 22:24:23 +00:00
auto fs(alias f)(in int[] s) pure nothrow
2013-04-10 23:57:08 -07:00
if (isCallable!f && ParameterTypeTuple!f.length == 1) {
2013-10-27 22:24:23 +00:00
return s.map!f;
2013-04-10 23:57:08 -07:00
}
int f1(in int x) pure nothrow { return x * 2; }
int f2(in int x) pure nothrow { return x ^^ 2; }
2013-10-27 22:24:23 +00:00
alias fsf1 = fs!f1;
alias fsf2 = fs!f2;
2013-04-10 23:57:08 -07:00
void main() {
2013-10-27 22:24:23 +00:00
foreach (const d; [[0, 1, 2, 3], [2, 4, 6, 8]]) {
d.fsf1.writeln;
d.fsf2.writeln;
2013-04-10 23:57:08 -07:00
}
}