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.

Template std.functional.partial

Partially applies fun by tying its first argument to arg.

template partial(alias fun, alias arg) ;

Contained Functions

NameDescription
partial

Parameters

NameDescription
fun A callable
arg The first argument to apply to fun

Returns

A new function which calls fun with arg plus the passed parameters.

Example

int fun(int a, int b) { return a + b; }
alias fun5 = partial!(fun, 5);
writeln(fun5(6)); // 11
// Note that in most cases you'd use an alias instead of a value
// assignment. Using an alias allows you to partially evaluate template
// functions without committing to a particular type of the function.

Example

// Overloads are resolved when the partially applied function is called
// with the remaining arguments.
struct S
{
    static char fun(int i, string s) { return s[i]; }
    static int fun(int a, int b) { return a * b; }
}
alias fun3 = partial!(S.fun, 3);
writeln(fun3("hello")); // 'l'
writeln(fun3(10)); // 30

Authors

Andrei Alexandrescu

License

Boost License 1.0.