Live data from Hacker News

G()('al')

github.com

41–49 of 49 posts

Re: G()('al')

#41
Quite trivial in idiomatic Scala:

  case class Goal(count: Int) {
    def apply() = Goal(count + 1)
    def apply(s: String) = s"g${"o" * count}$s"
  }

  object Goal {
    def g() = Goal(1)
  }

Non-idiomatic shorter answer:

  case class g(p: String = "go") {
    def apply() = g(p + "o")
    def apply(s: String = "o") = p + s
  }

Re: G()('al')

#42

Earlier quoted context omitted.

Just to clarify, I think it's a non-solution because the g()('al') is never actually executed - grep does all the work. You can add as many parenthesis as you want. The "exit" is there because you'll get a syntax error if you try to execute g()()('al'). g()('al') by itself does nothing (I'm not even sure why it's not flagged as a syntax error).

The three people who are judging this had a long discussion over this case, and basically decided that since it is in the file, it's fine. We frown on it a little because it's basically self-modifying code, but it's better than nothing for bash.

I actually prefer it vs. frowning on it for the record ;)

Re: G()('al')

#44

Quite trivial in idiomatic Scala: case class Goal(count: Int) { def apply() = Goal(count + 1) def apply(s: String) = s"g${"o" * count}$s" } object Goal { def g() = Goal(1) } Non-idiomatic shorter answer: case class g(p: String = "go") { def apply() = g(p + "o") def apply(s: String = "o") = p + s }

This looks great! Can you submit a pull request? These things are coming in really fast and its way easier to take that way.

Re: G()('al')

#45

The ruby non-solution seen here: https://github.com/eatnumber1/goal/blob/master/non-solutions... is actually doable if you parse the error.

But it requires eval-ing a string literal instead of actually having g()("al") as an expression itself

Re: G()('al')

#46

Quite trivial in idiomatic Scala: case class Goal(count: Int) { def apply() = Goal(count + 1) def apply(s: String) = s"g${"o" * count}$s" } object Goal { def g() = Goal(1) } Non-idiomatic shorter answer: case class g(p: String = "go") { def apply() = g(p + "o") def apply(s: String = "o") = p + s }

Also add

g(s: String) = "gal"

to handle the case of no 'o's.

Re: G()('al')

#47
post #35

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"…

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.

Re: G()('al')

#49

  # goal.rb
  module G
    def [](o=nil)
      o ? "g#{values[0].to_s}al" : ({ :n => values[0].to_s + "o" }.extend G)
    end
  end

  g = {}.extend G
  
  eval DATA.read.gsub("(", "[").gsub(")", "]")
  
__END__

  gal = g('al')
  p gal
  
  goal = g()('al')
  p goal

  goooooooal = g()()()()()()()('al')
  p goooooooal
  
  # ruby goal.rb
  # "gal"
  # "goal"
  # "goooooooal"

# https://gist.github.com/morganhankins/de13ee378907143a7789
Post reply on HN