Wither Self (shortish)
Chris Pressey
cpressey@REDACTED
Thu Sep 11 06:44:26 CEST 2003
On Wed, 10 Sep 2003 18:51:40 +0200 (MET DST)
Richard Carlsson <richardc@REDACTED> wrote:
> 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,
> do_stuff(F).
>
> 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.
It's a nice technique, but no, it doesn't really help me here.
The first issue is that I am very strongly against short (cryptic!)
names. (I suppose that might make me a minority in Erlang circles, but
after working on certain legacy source code with variables with names
like O1I1K in it, no amount of peer pressure could make me reconsider my
stance :) So instead of
h(X, Y, A, B, C)
I would have something more like
button_pressed(Button, Window, CList, ProgressBar, FileOptions)
That's bad enough; but the second issue is that, every time I have to
pass another parameter or a different parameter to the callback, I would
have to change the parameter list in *two* places in the source,
compared to zero with just the fun.
So all in all I just don't feel it's worth it in this case.
> 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) ->
> ...
I usually do that these days, too, to save the export declaration;
although IIRC the first time I tried it (I think it was with GTK), it
didn't work at all, so I was slow to adopt it after that.
-Chris
More information about the erlang-questions
mailing list