[erlang-questions] Learning erlang - is this an efficient way to build a list?
G Bulmer
gbulmer@REDACTED
Sat Oct 13 17:13:52 CEST 2007
An alternative is just
[spawn(fun ring:recv/0) || lists:seq(2,10)]
> Date: Fri, 12 Oct 2007 12:13:48 -0700 (PDT)
> From: graemebe <graemebe@REDACTED>
> Subject: [erlang-questions] Learning erlang - is this an efficient way
> to build a list?
> ...
> % Spawn some processes and send a message to each.
> %
> start() ->
> M=10,
> P=ring:spawnem(M),
> [X ! "hello world!" || X <- P].
>
> % Function to Spawn N processes
> %
> spawnem(N) when is_integer(N), N > 0 -> spawnem_acc(N, [spawn(fun
> ring:recv/0)]).
> spawnem_acc(N, L) when is_integer(N), N > 1 -> spawnem_acc(N-1,
> [spawn(fun
> ring:recv/0)] ++ L);
> spawnem_acc(1, L) -> L.
>
As it stands, an alternative is to replace spawnem and spawnem_acc
with a list comprehension:
start() ->
M=10,
P=[spawn(fun ring:recv/0) || _N <- lists:seq(2,10)]), %% <<---
changed
[X ! "hello world!" || X <- P].
and delete spawnem, and spawnem_acc.
GB
More information about the erlang-questions
mailing list