Created
March 17, 2018 15:55
-
-
Save abhin4v/3f2109702d4cbc6239854213e1386eaf to your computer and use it in GitHub Desktop.
PingPong in Elixir
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 PingPong do | |
def ping(parent_pid) do | |
receive do | |
{_, _, 0} -> send parent_pid, :done | |
{pid, :pong, n} -> | |
IO.puts("ping: " <> Integer.to_string(n)) | |
send pid, {self(), :ping, n-1} | |
ping parent_pid | |
end | |
end | |
def pong(parent_pid) do | |
receive do | |
{_, _, 0} -> send parent_pid, :done | |
{pid, :ping, n} -> | |
IO.puts("pong: " <> Integer.to_string(n)) | |
send pid, {self(), :pong, n-1} | |
pong parent_pid | |
end | |
end | |
def start(count) do | |
ping_pid = spawn(PingPong, :ping, [self()]) | |
pong_pid = spawn(PingPong, :pong, [self()]) | |
send ping_pid, {pong_pid, :pong, count} | |
receive do | |
:done -> IO.puts("Done") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment