optimization of list comprehensions

Sean Hinde <>
Mon Feb 27 00:33:52 CET 2006


On 26 Feb 2006, at 13:19, Ulf Wiger ((AL/EAB)) wrote:

>
> Samuel Rivas wrote:
>>
>> Ulf Wiger (AL/EAB) wrote:
>
> Because
>
>   [ets:insert(T, {K,V}) || {K,V} <- L,
>                            17 > V, V > 144]
>
> is decidedly more concise and readable than
>
>   lists:foreach(fun({K,V}) when 17 > V, V > 144 ->
>                       ets:insert(T, {K, V});
>                    (_) -> ok
>                 end, L)
>
> 42 characters vs. 73, or seen another way:
> 7 characters of overhead vs 38.
>
> I don't fault programmers for choosing the
> construct that's both easiest to write and
> read, even if it means building a list unnecessarily.

Personally, I find the foreach version easier to read. It is more  
akin to reading an english sentence than decoding some strange proof  
theorum or other mathematical construct.

Sean



More information about the erlang-questions mailing list