optimization of list comprehensions

Matthias Lang matthias@REDACTED
Mon Mar 6 23:13:52 CET 2006


 > Serge Aleynikov <serge@REDACTED> wrote:

 > 	Why not have a similar concise representation for the following case?
 > 	
 > 	lists:foldl(fun(I, Acc) -> Acc+I end, 0,
 > 	  lists:filter(fun(N) -> N > 10 end,
 > 	    lists:seq(1, 20)).

Richard A. O'Keefe writes:

 > Well, we can improve that:
 > 
 > 	lists:foldl(fun(I, Acc) -> Acc+I end, 0,
 > 	  [I || I <- lists:seq(1, 20), I > 10])
 > 
 > and indeed we can improve it to
 > 
 > 	lists:sum([I || I <- lists:seq(1, 20), I > 10])
 > 
 > which is pretty hard to improve on, no?

Oh, I don't know. If short is all that matters, how about:

  lists:sum([I || I <- lists:seq(11, 20)]).
  155

and indeed we can improve it to:

  lists:sum(lists:seq(11, 20)).
  155

which is pretty hard to improve on without entering _indisputably_
useless territory:

  lists:sum("155").
  155

Matthias



More information about the erlang-questions mailing list