Last active
December 26, 2015 02:09
-
-
Save radekstepan/7076126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule C do | |
def map([], _), do: [] | |
def map([ head | tail ], func) do: | |
[ func.(head), map(tail, func) ] | |
end | |
def child(element, func, parent) do: | |
parent <- { self, func.(element) } | |
end | |
def spawn_children(collection, func) do | |
map collection, fn element -> spawn(__MODULE__, :child, [ element, func, self ]) end | |
end | |
def collect_results(pids) do | |
map pids, fn pid -> receive do: ( {^pid, value} -> value ) end | |
end | |
def pmap(collection, func) do | |
collection |> spawn_children(func) |> collect_results | |
end | |
end | |
IO.inspect C.map [1,2,3,4,5], &1*&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment