Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Created February 18, 2020 15:31
Show Gist options
  • Select an option

  • Save benoittgt/489096ff081c9ac8232284f44440167b to your computer and use it in GitHub Desktop.

Select an option

Save benoittgt/489096ff081c9ac8232284f44440167b to your computer and use it in GitHub Desktop.
Ping Pong in elixir
defmodule PingPong do
def random do
time = :rand.uniform(1000)
:timer.sleep(time)
time
end
def ping do
receive do
{pong_pid} ->
IO.puts ("Ping")
random()
send pong_pid, { self() }
ping()
end
end
def pong do
receive do
{ping_pid} ->
IO.puts ("Pong")
random()
send ping_pid, { self() }
pong()
end
end
def run do
ping_pid = spawn(PingPong, :ping, [])
pong_pid = spawn(PingPong, :pong, [])
send ping_pid, {pong_pid}
receive do
# but why?
end
end
end
PingPong.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment