curry

Takes a function of (potentially) many arguments, and returns a function taking one argument and returns a callable taking the rest. f(x, y) == curry(f)(x)(y)

template curry(alias F)
curry
()
if (
Parameters!F.length
)

Parameters

F

a function taking at least one argument

Return Value

A single parameter callable object

Examples

int f(int x, int y, int z)
{
    return x + y + z;
}
auto cf = curry!f;
auto cf1 = cf(1);
auto cf2 = cf(2);

assert(cf1(2)(3) == f(1, 2, 3));
assert(cf2(2)(3) == f(2, 2, 3));
//works with callable structs too
struct S
{
    int w;
    int opCall(int x, int y, int z)
    {
        return w + x + y + z;
    }
}

S s;
s.w = 5;

auto cs = curry(s);
auto cs1 = cs(1);
auto cs2 = cs(2);

assert(cs1(2)(3) == s(1, 2, 3));
assert(cs1(2)(3) == (1 + 2 + 3 + 5));
assert(cs2(2)(3) ==s(2, 2, 3));

Meta