public string PrintTree()
        {
            StringBuilder s   = new StringBuilder();
            List <string> str = new List <string>();

            //rescursive lambdas for fun and profit
            //gets a list of decorators starting with the tree and building the list up from there
            SelfApplicable <List <string> > printHelper = (fun, y, t) =>
            {
                if (t._decorator == null)
                {
                    y.Add(t._name);
                }
                else
                {
                    fun(fun, y, t._decorator);
                    y.Add(t._name);
                }
                return(y);
            };

            str = printHelper(printHelper, str, this);

            s.Append(str[0]);
            s.Append(" tree decorated with ");
            //to handle if the tree has a star
            if (_hasStar)
            {
                s.Append(Star.Name);
            }


            for (int i = 1; i < str.Count; i++)
            {
                s.Append(", " + str[i]);
            }
            s.Append(" costs $");
            s.Append(this.Cost);


            return(s.ToString());
        }
Exemple #2
0
 public static OUTPUT selfApply <OUTPUT>(this SelfApplicable <OUTPUT> @this) => @this(@this);
Exemple #3
0
 public static void Render <T>(this HtmlHelper helper, T model, SelfApplicable <T> f)
 {
     f(f, model);
 }
 public static object Render <T>(this IHtmlHelper helper, T model, SelfApplicable <T> f)
 {
     return(f(f, model));
 }
 static Func <T1, TResult> Make <T1, TResult>(SelfApplicable <T1, TResult> self)
 {
     return((x) => self(self, x));
 }
Exemple #6
0
        static void Main(string[] args)
        {
            // The Y combinator
            SelfApplicable <Func <Func <Func <int, int>, Func <int, int> >, Func <int, int> > > Y = y => f => x => f(y(y)(f))(x);

            // The fixed point generator
            var Fix = Y(Y);

            // The higher order function describing factorial
            Func <Func <int, int>, Func <int, int> > F = fac => x => x == 0 ? 1 : x *fac(x - 1);

            // The factorial function itself
            var factorial = Fix(F);

            for (int j = 0; j < 1000; j++)
            {
                for (var i = 0; i < 12; i++)
                {
                    Console.WriteLine(factorial(i));
                }
            }
        }