-
Notifications
You must be signed in to change notification settings - Fork 44
UsageCurry
Jay Tuley edited this page Mar 19, 2018
·
3 revisions
Dynamic.Curry(object target, int? totalArgCount =null)
and then Dynamic.Curry(Delegate target)
because delegates will always know their argument count for currying.
If you are currying a method or a dynamic object you need to provide a count
or it will assume the 2nd call is providing the last argument(s) and resolve the invocation at that point.
Func<int, int, int, int, int> tAdd = (x, y, z, bbq) => x + y + z +bbq; //Original function
var curry = Dynamic.Curry(tAdd); // curry
curry = curry(4);
curry = curry(5);
curry = curry(6);
int result = curry(20); //35!
Func<int, int, int, int, int> add = (x, y, z, bbq) => x + y + z +bbq; //Original function
Func<int, Func<int, Func<int, Func<int, int>>>> curry0 = Dynamic.Curry(add); // curry and then implicit cast
var curry1 = curry0(4);
var curry2 = curry1(5);
var curry3 = curry2(6);
var result = curry3(20); //35!
var curry = Dynamic.Curry(Console.Out,5).WriteLine(); // curry method target include argument count
curry = curry("Test {0}, {1}, {2}, {3}");
curry = curry("A");
curry = curry("B");
curry = curry("C");
curry("D"); //Test A, B, C, D!
var staticContext = InvokeContext.CreateStatic;
var curry = Dynamic.Curry(staticContext(typeof(string)),5).Format(); // curry method target include argument count
curry = curry("Test {0}, {1}, {2}, {3}");
curry = curry("A");
curry = curry("B");
curry = curry("C");
string result =curry("D"); //Test A, B, C, D!
public class Adder:DynamicObject{
public override bool TryInvoke(InvokeBinder binder, object[] args, out object result){
result = args.Sum(it=>(int)it);
return true;
}
}
var add = new Adder();
var curry = Dynamic.Curry(add,4); // curry specify argument count
curry = curry(4);
curry = curry(5);
Func<int,int> curryLast = curry(6); //convert to a function for fun
var result = curryLast(20); //35!
Although the keyword is curry, it will work as a partial apply too
Func<int, int, int, int, int> tAdd = (x, y, z, bbq) => x + y + z +bbq; //Original function
var partialApply = Dynamic.Curry(tAdd); // curry
partialApply = partialApply(4,5);
int result = partialApply(6,20);