[erlang-questions] if vs. case (long)
Jay Nelson
jay@REDACTED
Thu Mar 13 14:13:45 CET 2008
On Mar 13, 2008, at 1:12 AM, Mats Cronqvist wrote:
> Ulf Wiger wrote:
>>
>> A colleague of mine would most definitely write this as
>>
>> DebugOn andalso log(Var).
>>
That's definitely shorter, although Mats points out the returning a
boolean issue. Not a bad solution and most telling of its intent,
including any side-effects caused by log/1.
>
> that's too verbose, and log/1 has to return a boolean. here's the
> Right Way(tm);
>
> [log(Var) || DebugOn].
>
> mats
>
> p.s. sarcasm! this construct is only to be used in obfuscated
> erlang competitions!
>
>
I tend to forget about the ways to abuse list comprehensions. I
don't mind adopting a particular idiomatic odd construct usage if it
has the following characteristics:
1) It is used to mean precisely one thing
2) You are consistent in your usage
3) Once figured out, it cannot be confused with normal usage of the
construct
4) Optional: put it in a macro and give it a better name
Since andalso is good enough and clear I would use it or put it in a
macro, although to me it has a PERL feel to it rather than an erlang
feel. I tend to expect erlang code to execute everything, rather
than to skip over some statements. I don't expect this in guard or
case situations where andalso is normally used.
But there may be another good (ab)use of list comprehensions that I
haven't tried yet ;-). And in the list comprehension case, the
statement _is_ executed. You expect some elements of the source list
to be skipped, possibly all of them.
The comprehension also can be expanded to a series of things:
[log(A) and log(B) || DebugOn]
How's this for obfuscation?
[[log(E) || E <- LogVars] || DebugOn]
jay
More information about the erlang-questions
mailing list