Skip to content

Instantly share code, notes, and snippets.

@bellthoven
Created November 19, 2012 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellthoven/4108853 to your computer and use it in GitHub Desktop.
Save bellthoven/4108853 to your computer and use it in GitHub Desktop.
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
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