[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.

V.

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

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

-- 
Dennis Byrne
_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list