Wither Self (shortish)

Richard Carlsson richardc@REDACTED
Wed Sep 10 18:51:40 CEST 2003

On Wed, 10 Sep 2003, Chris Pressey wrote:

> There are of course more ways to deal with it.  For example, factor
> everything that doesn't need to inherit bindings out of the fun, into
> utility functions.

Here's a nice way of combining funs and factoring:

	f(A,B,C) ->
		F = fun (X, Y) -> h(X, Y, A, B, C) end,

	h(X, Y, A, B, C) ->

	do_stuff(F) ->
	    P = ...,
	    Q = ...,
	    F(P, Q).

This way, the fun F has the right arity for the job (2, in this
case), and still carries all the extra info it needs (A, B, and C),
but you don't have to put all the code for the fun at the place
where you define it.

Don't know if this helps you, though. I tend to use this pattern
when I spawn processes:

	f(A, B, C) ->
	    Parent = self(),
	    Pid = spawn(fun () -> process(Parent, A, B, C) end),

	process(Parent, A, B, C) ->


Richard Carlsson (richardc@REDACTED)   (This space intentionally left blank.)
E-mail: Richard.Carlsson@REDACTED	WWW: http://user.it.uu.se/~richardc/
 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham

More information about the erlang-questions mailing list