I’ve been studying Erlang lately. It’s a lot of fun, and the language has some great features. One aspect of Erlang that appeals to me is pattern matching. You implement a function with different behaviors based on the arguments. For example, the
factorial function in Erlang can be defined as:
This makes for tight and elegant code; you don’t need to check your arguments and have branching logic. Of course in this case, I would probably add a constraint to the second one, i.e.
factorial(N) when N > 1 -> ..., since if
N < 1 then we’ll have a problem. Constraints are another great, expressive feature of the language.
Erlang takes pattern matching an extra step, borrowing from its ancestor Prolog: It uses the underscore character
_ (or any parameter name that starts with
_) as a throw-away parameter. So you can pattern match on a function with a signature like:
Again, I find this clear and elegant.
At work, I’m on a very large project that uses some jQuery, probably more jQuery than most of us would like. It seems like at least once a day, I get burned by writing code like this:
See the bug? What do you think this will log to console? The answer is
[0,1,2,3]. Oops. The bug is that jQuery’s
each function, for some God-forsaken reason, takes the index of the collection as its first argument, and the item as its second argument.
So I’ve borrowed the “I don’t care” parameter idea from Erlang, and now I write
jQuery.each like so:
I like the look of this–the underscore character suggests “I could be any value; I don’t care” to me. I prefer it to
function(index, item) where
index never gets used. It seems like
index should get used–it has a name!
There are some downsides to this approach:
function f(_, _, x). You can turn that off too, but probably shouldn’t.
So, caveat emptor.29 August 2013