Last active
June 6, 2016 02:47
-
-
Save samjarman/de7dd2337b425d1499cce16d0f1a5831 to your computer and use it in GitHub Desktop.
Returns a new list with the function applied to the list supplied
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
@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