[erlang-questions] Reassigning variables

Richard O'Keefe ok@REDACTED
Mon Mar 23 02:49:07 CET 2009


On 18 Mar 2009, at 7:07 pm, Matthew Dempsky wrote:
> Also, here's a list of a few other functions in OTP that use numbered
> variables:
>
>  - zip:put_z_files/6

Looking at that one in a little more detail, the key numbered
variable is Out0..Out6, and a trivial rewrite eliminates three
of its versions.  It's tracking the result of an Output function.
Since Erlang processes are lightweight, I would be very tempted
to replace the Output *function* with an Output *process*,
which could eliminate the updates entirely.

This is Joe Armstrong's well known "object = Erlang process"
equivalence.

Typically, in an OO language we would have had

	obj = new Thingy(...);
	...
	obj.mutateOne(...);
	...
	obj.mutateTwo(...);
	...
	ans = obj.summary();

In a pure functional language we have

	State0 = new_Thingy_State(...),
	...
	State1 = mutate_One(State0, ...),
	...
	State2 = mutate_Two(State1, ...),
	...
	Ans = summary(State2)

In Erlang, we can do

	Origin = self(),
	Pid = spawn(fun () -> ... Origin ... end),
	...
	Pid!{mutate_One, ...},
	...
	Pid!{mutate_Two, ...},
	...
	Pid!Summary,
	receive {summary,Pid,Ans} -> ok end

This transformation would _work_ for the zip: module and it would
definitely simplify the code.  Not having tried it, I have no idea
what it would do to performance.  But it's already going through
enough layers of function calls that it might be OK.






More information about the erlang-questions mailing list