Error reported when using pmap
Patrice Rault
parault2@REDACTED
Wed Aug 30 21:36:34 CEST 2006
Hello,
Le mercredi 30 août 2006 à 16:22 +0800, Arbow a écrit :
> Hi All:
> I am new in Erlang, and try the pmap at
> http://www.erlang.org/ml-archive/erlang-questions/200606/msg00187.html:
>
> In file test.erl:
> -module(test).
> -export([pmap/2]).
>
> pmap(F, L) ->
> S = self(),
> Pids = list:map(
> fun(I) ->
> spawn(
> fun() ->
> do_f(S, F, I)
> end)
> end, L),
> gather(Pids).
>
> gather([H|T]) ->
> receive
> {H, Ret} -> [Ret|gather(T)]
> end;
> gather([]) ->
> [].
>
> do_f(Parent, F, I) ->
> io:format("Receive item ~w~n", [I]),
> Parent ! {self(), (catch F(I))}.
>
> In file testmain.erl:
> -module(testmain).
> -export([test3/0]).
>
> test3() ->
> MyList = [1,2,3,4,5,6,7,8,9,10],
> lists:map(fun(I) -> I*2 end, MyList).
>
> when I execute " testmain:test3(). ", I get errors:
>
> =ERROR REPORT==== 30-Aug-2006::16:21:51 ===
> Error in process <0.36.0> on node 'Erlide_2960df@REDACTED' with exit
> value:
> {undef,[{testmain,test3,[]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
>
> ** exited: {undef,[{testmain,test3,[]},
> {erl_eval,do_apply,5},
> {shell,exprs,6},
> {shell,eval_loop,3}]} **
>
> Anyone could help me? Thanks.
>
> Best regards.
> Arbow
Try this
-module(test_pmap).
-export([pmap/2]).
pmap(F, L) ->
S = self(),
Pids = lists:map( % list:map ??
fun(I) ->
spawn(
fun() ->
do_f(S, F, I)
end)
end, L),
gather(Pids).
gather([H|T]) ->
receive
{H, Ret} -> [Ret|gather(T)]
end;
gather([]) ->
[].
do_f(Parent, F, I) ->
%io:format("Receive item ~w~n", [I]),
Parent ! {self(), (catch F(I))}.
-module(testmain).
-export([test3/0]).
test3() ->
MyList = [1,2,3,4,5,6,7,8,9,10],
%lists:map(fun(I) -> I*2 end, MyList),
test_pmap:pmap(fun(I) -> I*2 end, MyList).
Erlang (BEAM) emulator version 5.5 [source] [async-threads:0] [hipe]
Eshell V5.5 (abort with ^G)
1> c(test_pmap).
{ok,test_pmap}
2> c(testmain).
{ok,testmain}
3> testmain:test3().
[2,4,6,8,10,12,14,16,18,20]
4> l(testmain).
{module,testmain}
5>
Regards.
More information about the erlang-questions
mailing list