Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public void TestCurryNamedMethods()
        {
            Person  adam = new Person();
            dynamic jump = Impromptu.Curry(adam).Jump();

            Assert.Throws <NotImplementedException>(() => jump(cheer: "yay", height: (uint)3));
        }
Exemplo n.º 3
0
        public void TestPococMethodPartialApply()
        {
            var tNewObj = new PocoAdder();
            var tCurry  = Impromptu.Curry(tNewObj).Add(4, 6);
            var tResult = tCurry();

            Assert.AreEqual(10, tResult);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        public static void Dynamic()
        {
            var curried = Impromptu.Curry(logger);

            var errorInProgram = curried("Program.cs");

            errorInProgram(12)("Invalid syntax");
            errorInProgram(16, "Missing return statement");
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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);
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 19
0
        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);
        }
Exemplo n.º 20
0
        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);
        }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 23
0
        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));
        }
Exemplo n.º 24
0
        // 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);
        }