Beispiel #1
0
        /// <summary>
        /// TODO make this closer to original code
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private int FactorialByStrongRecursion(int number)
        {
            var answer = new RecursionBuilder <FParams, FResult>()
                         .If((p) =>
            {
                return(p.n == 0);
            })
                         .Then((p, r) =>
            {
                return(new FResult()
                {
                    result = 1 * r.result
                });
            })
                         .Else((p, r) =>
            {
                return(new StackFrame <FParams, FResult>()
                {
                    Params = new FParams {
                        n = p.n - 1
                    },
                    Result = new FResult {
                        result = p.n * r.result
                    }
                });
            })
                         .Build()
                         .Run(new FParams {
                n = number
            });

            return(answer.result);
        }
Beispiel #2
0
        private int StrongRecursion(int input)
        {
            var builder = new RecursionBuilder <DemoParams, DemoResult>()
                          .If((p) =>
            {
                // Custom predicate for limiting condition by user
                return(p.N == 1);
            })
                          .Then((p, r) =>
            {
                // Custom logic by user
                return(new DemoResult()
                {
                    Res = 1 + r.Res
                });
            })
                          .Else((p, r) =>
            {
                // Custom logic by user
                var inputParams = (DemoParams)p;
                var prevResult  = (DemoResult)r ?? new DemoResult();
                return(new StackFrame <DemoParams, DemoResult>()
                {
                    Params = new DemoParams()
                    {
                        N = inputParams.N - 1
                    },
                    Result = new DemoResult()
                    {
                        Res = inputParams.N + prevResult.Res
                    }
                });
            });

            var recursion = builder.Build();
            var result    = recursion.Run(new DemoParams()
            {
                N = input
            });

            return(result.Res);
        }