mirror of
https://github.com/dlang/phobos.git
synced 2025-05-10 22:18:03 +03:00
added curry
This commit is contained in:
parent
4548c91a41
commit
abd1f93a75
1 changed files with 31 additions and 5 deletions
|
@ -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...)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue