View source code
Display the source code in std/functional.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.functional.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)

auto curry(alias F)()
if (isCallable!F && Parameters!F.length);

auto curry(T) (
  T t
)
if (isCallable!T && Parameters!T.length);

Parameters

NameDescription
F a function taking at least one argument
t a callable object whose opCall takes at least 1 object

Returns

A single parameter callable object

Example

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

writeln(cf1(2)(3)); // f(1, 2, 3)
writeln(cf2(2)(3)); // f(2, 2, 3)

Example

//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);

writeln(cs1(2)(3)); // s(1, 2, 3)
writeln(cs1(2)(3)); // (1 + 2 + 3 + 5)
writeln(cs2(2)(3)); // s(2, 2, 3)

Authors

Andrei Alexandrescu

License

Boost License 1.0.