Created
November 8, 2015 08:04
-
-
Save killme2008/ac34c43b457cd43d8490 to your computer and use it in GitHub Desktop.
Elixir process chain test.
This file contains hidden or 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(pid) do | |
receive do | |
n -> | |
send pid, n+1 | |
end | |
end | |
def create_processes(n) do | |
last = Enum.reduce 1..n, self(), fn (_, pid) -> | |
spawn(Chain, :counter, [pid]) | |
end | |
send last, 0 | |
receive do | |
final_ansower when is_integer(final_ansower) -> | |
"Result is #{inspect(final_ansower)}." | |
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