September 30, 2010

Compact Choices in Erlang

To avoid the verbose case statements in Erlang, I sometimes use this function:

ifc(true,  True,  _) -> True;
ifc(false, _, False) -> False;

So instead of writing this:

case proplists:is_defined(empty, Options) of
    true  -> [];
    false -> [some_default]
end

One can write this:

ifc(proplists:is_defined(empty, Options), [], [some_default])

The idea was to mimic the simplicity of C’s (and other languages) ? operator.


Erlang programming


Next post
Synchronized, Reliable Message Passing in Erlang To do proper, synchronized message passing you need three things: monitoring, timeout and a unique tag in the messages. The basic case is sending a...