diff --git a/std/functional.d b/std/functional.d index 72b5d4097..f55e50997 100644 --- a/std/functional.d +++ b/std/functional.d @@ -36,12 +36,9 @@ $(WEB erdani.org, Andrei Alexandrescu) module std.functional; -import std.typetuple; -import std.typecons; -import std.stdio; -import std.metastrings; +import std.metastrings, std.stdio, std.traits, std.typecons, std.typetuple; // for making various functions visible in *naryFun -import std.string, std.conv, std.math, std.contracts, std.algorithm, std.range; +import std.algorithm, std.contracts, std.conv, std.math, std.range, std.string; /** Transforms a string representing an expression into a unary @@ -297,6 +294,35 @@ template not(alias pred) bool not(T...)(T args) { return !pred(args); } } +/** +Curries $(D fun) by tying its first argument to a particular value. + +Example: + +---- +int fun(int a, int b) { return a + b; } +assert(curry!(fun, 5)(6) == 11); +---- + */ +template curry(alias fun, alias arg) +{ + ReturnType!fun curry(ParameterTypeTuple!(fun)[1] arg2) + { + return fun(arg, arg2); + } +} + +unittest +{ + static int f1(int a, int b) { return a + b; } + assert(curry!(f1, 5)(6) == 11); + int x = 5; + int f2(int a, int b) { return a + b; } + assert(curry!(f2, x)(6) == 11); + auto f3 = &curry!(f2, x); + assert(f3(6) == 11); +} + /*private*/ template Adjoin(F...) { template For(V...)