Skip to content

Instantly share code, notes, and snippets.

@bmitch
Created June 25, 2017 14:35
Show Gist options
  • Save bmitch/472586ee7da5d272c74c0a0c44257cc2 to your computer and use it in GitHub Desktop.
Save bmitch/472586ee7da5d272c74c0a0c44257cc2 to your computer and use it in GitHub Desktop.
chain.exs
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 (_, self) ->
spawn(Chain, :counter, [self])
end
send last, 0
receive do
final_answer when is_integer(final_answer) ->
"Result is #{inspect(final_answer)}"
end
end
# elixir --erl "+P 1000000" -r chain.exs -e "Chain.run(1_000_000)
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