May 2018
Intermediate to advanced
412 pages
9h 3m
English
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 ...
Read now
Unlock full access