Hacker News new | ask | show | jobs
by juol 4342 days ago
C#

    using System;
    using System.Text;

    namespace Goal
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(G("al"));
                Console.WriteLine(G()("al"));
                Console.WriteLine(G()()()()()("al"));
            }

            static StringBuilder accumulator = new StringBuilder();
            delegate dynamic GoalDelegate(string s = "o");

            static dynamic G(string s = "o")
            {
                switch (s)
                {
                    case "o":
                        accumulator.Append("o");
                        return (GoalDelegate)G;
                    case "al":
                        string result = "G" + accumulator.ToString() + "al";
                        accumulator.Clear();
                        return result;
                    default:
                        return null;
                }
            }
        }

    }
2 comments

Here's my C# solution that uses no global state, and features a cool (I think) variadic lambda.

    using System;
    using System.Linq;

    class Program {
        static void Main() {
            Console.WriteLine(g("al"));
            Console.WriteLine(g()("al"));
            Console.WriteLine(g()()("al"));
            Console.WriteLine(g()()()("al"));
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }

        delegate dynamic Goal(params string[] args);

        static dynamic g(string suffix = null, string prefix = "g") {
            if (suffix == "al") return prefix + suffix;
            return (Goal)(a => g(a.FirstOrDefault(), prefix + "o"));
        }
    }
It was a fun exercise. I found that creating a github pull request was not such a fun exercise, and took about 10x longer than the actual task at hand. Perhaps I could learn to do it more smoothly with practice.
Cool! Can you submit a pull request?