Devin Torres reminded me that every book in the Erlang space must, by law, include a parallel map function. Regular map returns the list that results from applying a function to each element of a collection. The parallel version does the same, but it applies the function to each element in a separate process.
| defmodule Parallel do |
| def pmap(collection, fun) do |
| me = self() |
| collection |
| |> Enum.map(fn (elem) -> |
| spawn_link fn -> (send me, { self(), fun.(elem) }) end |
| end) |
| |> Enum.map(fn (pid) -> |
| receive do { ^pid, result } -> result end |
| end) |
| end |
| end |
Our method contains two transformations (look for the |> operator). The first transformation maps collection into a list of PIDs, where each PID in the list runs the given function on an individual list element. If the collection contains 1,000 items, we’ll run 1,000 processes.
The second transformation converts the list of PIDs into the results returned by the processes corresponding to each PID in the list. Note how it uses ^pid in the receive block to get the result for each PID in turn. Without this we’d get back the results in random order.
But does it work?
| iex> c("pmap.exs") |
| [Parallel] |
| iex> Parallel.pmap 1..10, &(&1 * &1) |
| [1,4,9,16,25,36,49,64,81,100] |
That’s pretty sweet, but it gets better, as we’ll cover when we look at tasks and agents.