[erlang-questions] spawning processes and passing arguments

606 views
Skip to first unread message

deepblue

unread,
Feb 20, 2009, 4:25:33 PM2/20/09
to erlang-q...@erlang.org
I keep running into this. I want to spawn processes and pass arguments
to them without using the MFA form (module/function/arguments), so
basically without having to export the function I want to spawn with
arguments. I've gotten around this a few times using closures(fun's)
and having the arguments just be bound values outside the fun, but its
limiting my code structure... I've looked at the docs and spawn only
has the regular spawn/1 and the spawn/3 form, nothing else...

much appreciated
thanks
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions

Richard Carlsson

unread,
Feb 20, 2009, 5:07:56 PM2/20/09
to deepblue, erlang-q...@erlang.org
deepblue wrote:
> I keep running into this. I want to spawn processes and pass arguments
> to them without using the MFA form (module/function/arguments), so
> basically without having to export the function I want to spawn with
> arguments. I've gotten around this a few times using closures(fun's)
> and having the arguments just be bound values outside the fun, but its
> limiting my code structure... I've looked at the docs and spawn only
> has the regular spawn/1 and the spawn/3 form, nothing else...

By "using closures", I hope you mean something like this:

Pid = spawn(fun () -> any_function(Any, Number, Of, Arguments) end)

How would that be limiting to your code structure?

/Richard

deepblue

unread,
Feb 20, 2009, 5:52:26 PM2/20/09
to erlang-q...@erlang.org
actually Richard your way solves my problems :)
for some reason I was limiting myself to only using the closure as:
spawn(any_function(Any, Number, Of, Arguments)) which ofcourse doesnt
work
so I then did
spawn(any_function) and at the place of fun declaration I would have
external bound variables that the closure would refer to like so


BoundVar1 = 1,
BoundVar2 = 2,
Fun_toSpawn = fun()->BoundVar1+BoundVar2 end,
...
...
lists:forEach(
fun(X)->
%and this was limiting me since I needed to use X and couldnt
pass it in
%except if I declared the fun itself inside this forEach
callback which is very innefficient
spawn(Fun_toSpawn)

end,List).


it seems a lot can be done by nesting functions inside very simple
fun's and referencing parameters like that

thank you very much

> erlang-questi...@erlang.orghttp://www.erlang.org/mailman/listinfo/erlang-questions

deepblue

unread,
Feb 20, 2009, 5:55:54 PM2/20/09
to erlang-q...@erlang.org
actually Richard your way solves my problems :)
for some reason I was limiting myself to only using the closure as:
spawn(any_function(Any, Number, Of, Arguments)) which ofcourse doesnt
work
so I then did
spawn(any_function) and at the place of fun declaration I would have
external bound variables that the closure would refer to like so


BoundVar1 = 1,
BoundVar2 = 2,
Fun_toSpawn = fun()->BoundVar1+BoundVar2 end,
...
...
lists:forEach(
fun(X)->
%and this was limiting me since I needed to use X and couldnt
pass it in
%except if I declared the fun itself inside this forEach
callback which is very innefficient
spawn(Fun_toSpawn)

end,List).


it seems a lot can be done by nesting functions inside very simple
fun's and referencing parameters like that

thank you very much

On Feb 20, 5:07 pm, Richard Carlsson <richa...@it.uu.se> wrote:

> erlang-questi...@erlang.orghttp://www.erlang.org/mailman/listinfo/erlang-questions

Reply all
Reply to author
Forward
0 new messages