defmodule Parallel do
def pmap(collection, func) do
me = self()
collection
|> Enum.map(fn e ->
spawn_link(fn ->
send(me, {self(), func.(e)})
end)
end)
|> Enum.map(fn pid ->
receive do
{^pid, ret} -> ret
end
end)
end
def run(n) do
IO.inspect(:timer.tc(Parallel, :pmap, [1..n, &(&1 * &1)]))
end
end
运行: