|
|
|
|
|
by RootDynasty
4083 days ago
|
|
Here is another cool version of parallel map in Erlang that uses a fold instead of two parallel maps. parallel_map(Fun, List) ->
Last = lists:foldl(fun(Value, Parent) ->
spawn(fun() ->
MappedValue = Fun(Value),
receive
Rest -> Parent ! [MappedValue|Rest]
end
end)
end, self(), List),
Last ! [],
receive
Result -> Result
end.
It essentially sets up a chain of processes which pass their results to their parent when they finish their mapping operation. |
|
So instead of copying 2n elements over the processes, you'll be copying roughly 2nlogn (or is it n(n+1)/2 ?) elements around.