Skip to content

Instantly share code, notes, and snippets.

@dpritchett
Last active August 29, 2015 13:56
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 dpritchett/9008303 to your computer and use it in GitHub Desktop.
Save dpritchett/9008303 to your computer and use it in GitHub Desktop.
#---
# 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
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