[erlang-questions] At what point am I "playing compiler"

Valentin Micic v@REDACTED
Mon May 18 08:38:49 CEST 2009

Was it Joe's rule that goes like this:

"Make it run first, and then optimize later -- only if you have to"

Stick to this rule, and the life will be good to you.


-----Original Message-----
From: erlang-questions-bounces@REDACTED
[mailto:erlang-questions-bounces@REDACTED] On Behalf Of Dennis Byrne
Sent: 17 May 2009 07:12 PM
To: erlang-questions@REDACTED
Subject: [erlang-questions] At what point am I "playing compiler"

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 ->
min(_, Y) ->

max(X, Y) when X > Y ->
max(_, Y) ->

Dennis Byrne
erlang-questions mailing list

More information about the erlang-questions mailing list