[erlang-questions] At what point am I "playing compiler"
Dennis Byrne
dennisbyrne@REDACTED
Sun May 17 19:11:30 CEST 2009
The functions expressive/0 and efficient/0 have the same result.
Sometimes I prefer expressive syntax but I am concerned about
efficiency. Should Erlang developers worry about this or do any of
the compilers (or runtime) make these concerns obselete?
expressive() ->
List = [1,2,3],
Last = lists:last(List),
Min = lists:foldl(fun min/2, Last, List),
Max = lists:foldl(fun max/2, Last, List),
Sum = lists:foldl(fun sum/2, 0, List),
{Min, Max, Sum}.
efficient() ->
List = [1,2,3],
Last = lists:last(List),
lists:foldl(fun summary/2, {Last, Last, 0}, List).
summary(X, {Min, Max, Total}) ->
{min(X, Min), max(X, Max), Total + X}.
sum(X, Y) ->
X + Y.
min(X, Y) when X < Y ->
X;
min(_, Y) ->
Y.
max(X, Y) when X > Y ->
X;
max(_, Y) ->
Y.
--
Dennis Byrne
More information about the erlang-questions
mailing list