[erlang-questions] spawning processes and passing arguments

deepblue cktgatb@REDACTED
Fri Feb 20 23:52:26 CET 2009


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...@REDACTED> wrote:
> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questi...@REDACTED://www.erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list