Last active
August 29, 2015 13:56
-
-
Save dpritchett/9008303 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
#--- | |
# Excerpted from "Programming Elixir", | |
# published by The Pragmatic Bookshelf. | |
# Copyrights apply to this code. It may not be used to create training material, | |
# courses, books, articles, and the like. Contact us if you are in doubt. | |
# We make no guarantees that this code is fit for any purpose. # Visit http://www.pragmaticprogrammer.com/titles/elixir for more book information. | |
#--- | |
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 |
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 ParallelTest do | |
use ExUnit.Case | |
test "the truth" do | |
assert(true) | |
end | |
test "i can do math" do | |
result = Parallel.pmap 1..100000000, &(&1 * &1) | |
assert true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment