Skip to content

Instantly share code, notes, and snippets.

@radekstepan
Last active December 26, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radekstepan/7076126 to your computer and use it in GitHub Desktop.
Save radekstepan/7076126 to your computer and use it in GitHub Desktop.
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