Created
December 7, 2014 05:27
-
-
Save bcardarella/e3881e0a87fda58db64c 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
defmodule Chain do | |
def counter(next_pid) do | |
receive do | |
n -> | |
send next_pid, n + 1 | |
end | |
end | |
def create_processes(n) do | |
last = Enum.reduce 1..n, self, | |
fn (_, send_to) -> | |
spawn(Chain, :counter, [send_to]) | |
end | |
send last, 0 | |
receive do | |
final_answer when is_integer(final_answer) -> | |
"Result is #{inspect(final_answer)}" | |
end | |
end | |
def run(n) do | |
IO.puts inspect :timer.tc(Chain, :create_processes, [n]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment