public override bool TrySetMember(SetMemberBinder binder, object value) { var selectorTuple = Tuple.Create(binder.Name, Direction.In); var property = _properties[binder.Name]; var inTransformer = _inTransformers.ContainsKey(binder.Name) ? _inTransformers[binder.Name].Item2 : _identity; var interceptor = _propertyInterceptors.ContainsKey(selectorTuple) ? _propertyInterceptors[selectorTuple] : null; var transformedValue = inTransformer.FastDynamicInvoke(value); if (interceptor != null) { Action <object, object> meth = property.SetValue; Func <object, Action <object> > curry = Impromptu.Curry(meth); Func <object[], object> curriedMethod = args => { curry(_wrappedObject)(args[0]); return(null); }; interceptor.FastDynamicInvoke(new object [] { curriedMethod, new [] { transformedValue } }); } else { property.SetValue(_wrappedObject, transformedValue); } return(true); }
public void TestCurryNamedMethods() { Person adam = new Person(); dynamic jump = Impromptu.Curry(adam).Jump(); Assert.Throws <NotImplementedException>(() => jump(cheer: "yay", height: (uint)3)); }
public void TestPococMethodPartialApply() { var tNewObj = new PocoAdder(); var tCurry = Impromptu.Curry(tNewObj).Add(4, 6); var tResult = tCurry(); Assert.AreEqual(10, tResult); }
public void TestBasicConvertMoreCurryParamValueType() { Func <int, int, int, int> tAdd = (x, y, z) => x + y + z; Func <int, Func <int, int> > Curry1 = Impromptu.Curry(tAdd)(4); Func <int, int> Curry2 = Curry1(6); int tResult = Curry2(10); Assert.AreEqual(20, tResult); }
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { Delegate outTransformer; MethodInfo method; Delegate interceptor; Tuple <int, Delegate> inTransformer; try { var typeArgs = binder.GetTypeArguments(); var hasTypeArgs = typeArgs != null && typeArgs.Count > 0; var methodSelectorTuple = Tuple.Create(binder.Name, hasTypeArgs, args.Length); var selectorTuple = Tuple.Create(binder.Name, hasTypeArgs); method = _publicMethods[methodSelectorTuple]; outTransformer = _outTransformers.ContainsKey(selectorTuple) ? _outTransformers[selectorTuple] : _identity; interceptor = _interceptors.ContainsKey(selectorTuple) ? _interceptors[selectorTuple] : null; inTransformer = _inTransformers.ContainsKey(binder.Name) ? _inTransformers[binder.Name] : null; if (hasTypeArgs) { method = method.MakeGenericMethod(typeArgs.ToArray()); } } catch { result = null; return(false); } try { if (inTransformer != null) { args[inTransformer.Item1] = inTransformer.Item2.FastDynamicInvoke(args[inTransformer.Item1]); } object partialResult; if (interceptor != null) { Func <object, object[], object> meth = method.Invoke; Func <object, Func <object[], object> > curry = Impromptu.Curry(meth); var curriedMethod = curry(_wrappedObject); partialResult = interceptor.FastDynamicInvoke(new object[] { curriedMethod, args }); } else { partialResult = method.Invoke(_wrappedObject, args); } result = outTransformer.FastDynamicInvoke(partialResult); } catch (TargetInvocationException e) { throw e.InnerException; } return(true); }
public void TestBasicNamedCurry() { Func <int, int, int> tSub = (x, y) => x - y; var tCurriedSub7 = Impromptu.Curry(tSub)(arg2 : 7); var tResult = tCurriedSub7(arg1: 10); Assert.AreEqual(3, tResult); }
public void TestBasicConvertDelegateCurry() { Func <string, string, string> tAdd = (x, y) => x + y; var tCurriedAdd4 = Impromptu.Curry(tAdd)("4"); var tCastToFunc = (Func <string, string>)tCurriedAdd4; var tResult2 = tCastToFunc("10"); Assert.AreEqual("410", tResult2); }
public void TestBasicConvertDelegateCurryParamValueType() { Func <int, int, int> tAdd = (x, y) => x + y; var tCurriedAdd4 = Impromptu.Curry(tAdd)(4); Func <int, int> tCastToFunc = tCurriedAdd4; var tResult2 = tCastToFunc(10); Assert.AreEqual(14, tResult2); }
public void TestBasicConvertNonGenericDelegate() { Func <string, string, bool> tContains = (x, y) => y.Contains(x); var tCurriedContains = Impromptu.Curry(tContains)("it"); TestDeclaredDelagate tCastToDel = tCurriedContains; var tResult = tCastToDel("bait"); Assert.AreEqual(true, tResult); }
public void UnboundedCurry() { var tNewObject = Impromptu.Curry(Build.NewObject); var tCurriedNewObject = tNewObject(One: 1); var tResult = tCurriedNewObject(Two: 2); Assert.AreEqual(1, tResult.One); Assert.AreEqual(2, tResult.Two); }
public void TestBasicDelegateCurry() { Func <int, int, int> tAdd = (x, y) => x + y; var tCurriedAdd4 = Impromptu.Curry(tAdd)(4); var tResult = tCurriedAdd4(6); Assert.AreEqual(10, tResult); }
public void TestBasicConvertDelegateCurryReturnValueType() { Func <string, string, int> tAdd = (x, y) => Int32.Parse(x) + Int32.Parse(y); var tCurriedAdd4 = Impromptu.Curry(tAdd)("4"); Func <string, int> tCastToFunc = tCurriedAdd4; var tResult2 = tCastToFunc("10"); Assert.AreEqual(14, tResult2); }
public static void Dynamic() { var curried = Impromptu.Curry(logger); var errorInProgram = curried("Program.cs"); errorInProgram(12)("Invalid syntax"); errorInProgram(16, "Missing return statement"); }
public void TestBasicConvertNonGenericDelegateAction() { var tBool = false; Action <string, string> tContains = (x, y) => tBool = y.Contains(x); var tCurriedContains = Impromptu.Curry(tContains)("it"); TestRunDelagate tCastToDel = tCurriedContains; tCastToDel("bait"); Assert.AreEqual(true, tBool); }
public void TestBasicConvertMoreMoreCurryParamValueType() { Func <int, int, int, int, int> tAdd = (x, y, z, bbq) => x + y + z + bbq; Func <int, Func <int, Func <int, Func <int, int> > > > Curry0 = Impromptu.Curry(tAdd); var Curry1 = Curry0(4); var Curry2 = Curry1(5); var Curry3 = Curry2(6); var tResult = Curry3(20); Assert.AreEqual(35, tResult); }
public void TestStaticMethodCurry3() { var tFormat = Enumerable.Range(0, 100).Aggregate(new StringBuilder(), (result, each) => result.Append("{" + each + "}")).ToString(); dynamic curriedWrite = Impromptu.Curry(Console.Out, 101).WriteLine(tFormat); Func <dynamic, int, dynamic> applyArgs = (result, each) => result(each.ToString()); Enumerable.Range(0, 100).Aggregate((object)curriedWrite, applyArgs); }
public void TestStaticMethodCurry2() { object curriedJoin = Impromptu.Curry((StaticContext)typeof(string), 51).Join(","); Func <dynamic, int, dynamic> applyFunc = (result, each) => result(each.ToString()); string final = Enumerable.Range(1, 100) .Where(i => i % 2 == 0) .Aggregate(curriedJoin, applyFunc); Console.WriteLine(final); }
public void BoundedCurryCont() { var tNewObject = Impromptu.Curry(Build.NewObject, 3); tNewObject = tNewObject(One: 1); tNewObject = tNewObject(Two: 2); var tResult = tNewObject(Three: 3); Assert.AreEqual(1, tResult.One); Assert.AreEqual(2, tResult.Two); Assert.AreEqual(3, tResult.Three); }
public void UnboundedPartialApply() { var tNewObject = Impromptu.Curry(Build.NewObject); tNewObject = tNewObject(One: 1, Two: 2); var tResult = tNewObject(Three: 3, Four: 4); Assert.AreEqual(1, tResult.One); Assert.AreEqual(2, tResult.Two); Assert.AreEqual(3, tResult.Three); Assert.AreEqual(4, tResult.Four); }
public void TestStaticMethodCurry() { var curry = Impromptu.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"); Assert.AreEqual("Test A, B, C, D", result); }
public void TestDynamicMethodCurry() { var tNewObj = Build.NewObject(Add: Return <int> .Arguments <int, int>((x, y) => x + y)); var tCurry = Impromptu.Curry(tNewObj).Add(4); var tResult = tCurry(10); Assert.AreEqual(14, tResult); //Test cached invocation; var tResult2 = tCurry(30); Assert.AreEqual(34, tResult2); }
public void TestPococMethodCurry() { var tNewObj = new PocoAdder(); var tCurry = Impromptu.Curry(tNewObj).Add(4); var tResult = tCurry(10); Assert.AreEqual(14, tResult); //Test cached invocation; var tResult2 = tCurry(30); Assert.AreEqual(34, tResult2); }
public void BasicCurryTest() { Func <int, double, float, double> adder = (x, y, z) => x + y + z; var curried = Impromptu.Curry(adder); Assert.AreEqual(6, curried(1, 2, 3)); Assert.AreEqual(6, curried(1, 2)(3)); Assert.AreEqual(6, curried(1)(2, 3)); Assert.AreEqual(6, curried(1)(2)(3)); }
// Calling an extension method defined in GameAPI. If a valid proxy delegate is found will be cached globally among all Agents // - has no runtime check if parameters of the cached method matches // - no overloading supported yet. // - caching might stop adopting changes during runtime, no? public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { var methodName = binder.Name; if (!AgentSkills.Methods.ContainsKey(methodName)) { var api = typeof(AgentAPI).GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); var methods = (from method in api where method.Name == methodName select method).ToArray(); if (methods.Length > 1) { throw new Exception(methodName + "() is ambiguous, methods in AgentAPI cannot be overloaded yet."); } if (methods.Length == 0) { throw new Exception(methodName + "() not found. Define it in the partial class AgentAPI"); } var extensionMethod = methods.First <MethodInfo>(); Expression[] parameters = extensionMethod.GetParameters() .Select(p => Expression.Parameter(p.ParameterType, p.Name)) .ToArray(); if (parameters.Length != args.Count() + 1) { throw new Exception("Something seems wrong with the arguments for " + binder.Name + "()"); } var call = Expression.Call(extensionMethod, parameters); var func = Expression.Lambda(call, binder.Name, false, (ParameterExpression[])parameters).Compile(); AgentSkills.Methods.Add(methodName, func); } var curry = Impromptu.Curry(AgentSkills.Methods[methodName], args.Length + 1); curry = curry(this); // first argument is the instance of IAgent foreach (var arg in args) { curry = curry(arg); // followed by whatever parameters you sent in } result = curry; return(true); }