[erlang-questions] question re parallel map code on pg 366 of "Programming Erlang"

Howard Yeh <>
Thu Jan 22 05:08:07 CET 2009


On Wed, Jan 21, 2009 at 7:51 PM, David Cabana <> wrote:
> I have a question re Joe's parallel map code on page 366 of Programming Erlang.
> Here's the code, modulo a couple of variable name changes.
>
> pmap(F, Arglist) ->
>    S = self(),
>    TaskID = make_ref(),
>    Workers = lists:map( fun(X) ->
>                                 spawn( fun() -> do_F(S, TaskID, F, X )  end)
>                         end, Arglist),
>    gather(Workers, TaskID).
>
> do_F(Caller, TaskID, F, X) ->
>    Caller ! {self(), TaskID, catch(F(X))}.
>
> gather([W|R], TaskID) ->
>    receive
>        {W, TaskID, Val}->
>            [Val | gather(R, TaskID)]
>    end;
>
> gather([], _) ->
>    [].
>
> My question concerns the use of catch inside the do_F function. Here's what Joe
> says on page 367:
>
> "In pmap we use catch(F(H)) when we map the function over the list. In
> map we just
> use F(H). This is because we want to make sure pmap terminates
> correctly in the case
> where the computation of F(H) raises an exception."
>
> I don't get it.
>
> If the computation of F(H) raises an exception, presumably it would do
> so whether F was called by
> map or by pmap. Why catch the exception in the one case but not the other?
> Why is it important that pmap terminate normally, but not so that map
> terminate normally?
>
i think the difference is cultural. This example demonstrates a simple
mapreducesque thing.

pmap (mapreduce) prefers to skip over a few instances of errors so
long as everything else is ok.

map is more programming-in-the-small, so it would make sense to raise
(just as you would in a loop).

so pmap and map has the same semantic, but different pragmatics.

> Thanks,
> drc
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list