Created
November 19, 2012 03:56
-
-
Save bellthoven/4108853 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 HelloWorld do | |
def say do | |
world_pid = spawn(HelloWorld, :world, []) | |
spawn(HelloWorld, :hello, [1000, world_pid]) | |
end | |
def hello(n, world_pid) when n > 0 do | |
IO.puts "Hello, " | |
world_pid <- {:world, Process.self} | |
receive do | |
{:hello} -> hello(n - 1, world_pid) | |
end | |
end | |
def hello(0, world_pid) do | |
world_pid <- :done | |
end | |
def world do | |
receive do | |
{:world, hello_pid} -> | |
IO.puts "World" | |
hello_pid <- {:hello} | |
world | |
:quit -> :quit | |
end | |
end | |
end |
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 HelloWorld do | |
def say do | |
Process.register world_pid_name, spawn(HelloWorld, :world, []) | |
Process.register hello_pid_name, spawn(HelloWorld, :hello, []) | |
end | |
def hello, do: hello(1000) | |
def hello(n) when n > 0 do | |
IO.puts "Hello, " | |
world_pid <- :world | |
receive do | |
:hello -> hello(n - 1) | |
end | |
end | |
def hello(0) do | |
world_pid <- :quit | |
end | |
def world do | |
receive do | |
:world -> | |
IO.puts "World" | |
hello_pid <- :hello | |
world | |
:quit -> :quit | |
end | |
end | |
defp world_pid_name, do: :hello_world_world_pid | |
defp hello_pid_name, do: :hello_world_hello_pid | |
defp world_pid, do: Process.whereis(world_pid_name) | |
defp hello_pid, do: Process.whereis(hello_pid_name) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment