Hacker News new | ask | show | jobs
by dave_ops 3873 days ago
Seems reasonable to me... except I don't see the point of having foo/1 and foo1/1. The author could just have easily done:

  foo(bar) -> void;
  foo(X) -> undefined.
without the need for the proxy function:

  foo(X) -> foo1(X).
2 comments

I think it was for demonstration. The example certainly is a bit contrived but the general idea is that pattern matching in function heads is preferred over pattern matching in case expressions by most experienced Erlang developers I know and certainly by the author.

It's not a hard rule but there are definitely times when giving a name to a set of patters is better than the anonymous case. Some other languages use things like let-in and with to achieve similar ideas but Erlang is a rather simple language so it just reuses function heads instead of nesting definitions.

Yeah, it makes for some very interesting uses cases for code-generation.

I've converted entire databases of mostly static data into nothing but a bunch of exact-match function head signatures and let one of the fastest paths in the VM be my "query planner".

It's insanely fast (at the expense of compile time) and the generated code is really easy to read, trace, and debug.

Intersting. Do you have an example somewhere?
The point is to show an example of how a mechanical translation could work from one form to another. It is like an intermediate step if you wish. You'd instead do that code snippet you wrote first of course. But yeah looking back at comments here I think they've confused others with that code as well it seems ;-)