Skip to content

Instantly share code, notes, and snippets.

@samjarman
Last active June 6, 2016 02:47
Show Gist options
  • Save samjarman/de7dd2337b425d1499cce16d0f1a5831 to your computer and use it in GitHub Desktop.
Save samjarman/de7dd2337b425d1499cce16d0f1a5831 to your computer and use it in GitHub Desktop.
Returns a new list with the function applied to the list supplied
@doc """
This returns a new list with the function applied to the list supplied.
Examples:
iex> Misc.pmap([1,2,3,4], fn(n) -> n * n end)
[1,4,9,16]
iex> Misc.pmap([1,2,3,4], fn(n) -> n + 1 end)
[2,3,4,5]
"""
def pmap(list, func) do
me = self
list
|> Enum.map(fn(elem) -> spawn(fn -> send(me, {self, func.(elem)}) end) end)
|> Enum.map(fn(pid) ->
receive do
{pid, result} -> result
end
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment