Hacker News new | ask | show | jobs
by pmontra 2741 days ago
Thanks. Destructuring is nice and I'll check your link.

I'm using Elixir's pattern matching also in function definitions, to do without ifs and switches. A trivial example with Elixir syntax (hopefully correct).

   def div(a, 0), do: {:error, "can't divide by 0"}
   def div(a, 1), do: {:ok, a}
   def div(0, b), do: {:ok, 0}
   def div(a, b), do: {:ok, a/b}
I'd like to have a pattern matching like that in Ruby as well. A more real world example:

   def something(%{"a" => %{"b" => b, "c" => c}}), do {:ok, b + c}
   def something(_), do {:error, "a didn't contain b and c"}
Much more readable than having to write ifs inside the function/method.

All considered, I find Ruby a more pleasant language than Elixir because of syntax, verbosity, state keeping (probably unfair, because OO is made for that vs GenServers), deployment story. I'd like to keep using it for applications that can scale by adding processes, but pattern matching is a killer feature.

Pipelines are important in Elixir because it's functional. It would be a pain to code without them. Object oriented languages can somewhat pipeline by calling methods on results of previous methods, provided a consistent design of classes (each.map.uniq.sort). It seems that in Ruby it could remove lots of useless variables. A lot less head scratching to find sensible variable names, faster programming. I hope it gets into the language or that this gem gets popular.

1 comments

Thanks! Agreed it feels like a good fit for Ruby as well!

RE the pattern matching stuff I think the destructuring concept could be applied to look something like:

    def div(*args)
      case args
      when Pattern{[a, 0]} then [:error, "can't divide by 0"]
      when Pattern{[a, 1]} then [:ok, Pattern.last.a]
      when Pattern{[0, b]} then [:ok, 0]
      when Pattern{[a, b]} then [:ok, Pattern.last.a / Pattern.last.b]
      end
    end

    def something(object)
      case object
      when Pattern{a[b, c]} then [:ok, Pattern.last.b + Pattern.last.c]
      else [:error, "a didn't contain b and c"]
      end
    end
Some shorthand to access `Pattern.last` e.g. "$~" for regex would make things even nicer!