$> iex customstack.exs
iex> Stack.CustomServer.start []
iex> sned :custom_server, { :push, 10 }
iex> send :custom_server, { :self, :pop }
###iex manual receive
receive do
val -> IO.puts "Pop resulted in #{val}"
end
defmodule Stack.CustomServer do
def start(initial_stack) do
spawn_link fn ->
Process.register self, :custom_server
listen initial_stack
end
end
def listen(stack) do
receive do
after 2000 ->
IO.puts "Nothing to do"
listen stack
end
end
end
{:push, value} -> listen([value|stack])
send :custom_server, { :push, 10 }
{sender, :pop} -> handle_pop(sender, stack)
#...
def handle_pop(sender, [head|tail]) do
send sender, head
listen tail
end
send :custom_server, {self, :pop}