[erlang-questions] Re: Concept of Side-effect
Ulf Wiger
ulf.wiger@REDACTED
Fri Sep 18 15:03:12 CEST 2009
Jayson Vantuyl wrote:
> Actually, I'm not entirely sure that an exception would be a side
> effect. It doesn't get "returned" like a normal value, but I don't see
> that it changes state any more than a return value does.
It straddles the fence in some ways. Functions can be pure for all
legal input and raise exceptions for illegal input. This is fine,
but there are also functions that raise exceptions even for legal
input.
Consider the following code:
pmap(F, L) ->
Pids = [spawn_monitor(fun() ->
exit({ok, F(X)})
end) || X <- L],
[receive {'DOWN',Ref,_,_,R} -> unwrap(R) end
|| {_,Ref} <- Pids].
(A simplified version of slide 7 in this talk:
http://www.erlang-factory.com/upload/presentations/56/UlfWiger_ErlangMulticoreEF.pdf)
While it might seem contrived, the shell evaluator
does roughly the same thing: evaluates the expression
inside an exit() call in order to deliver the result and
die in one and the same action.
Such functions are only meaningful when considering their
side-effects - the exceptions.
BR,
Ulf W
--
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com
More information about the erlang-questions
mailing list