Skip to content

Instantly share code, notes, and snippets.

@nsomar
Created April 23, 2017 18:56
Show Gist options
  • Select an option

  • Save nsomar/a4d783c617d47dbac35a0714e0b789c5 to your computer and use it in GitHub Desktop.

Select an option

Save nsomar/a4d783c617d47dbac35a0714e0b789c5 to your computer and use it in GitHub Desktop.
defmodule Storage do
def start(initial) do
process = spawn_link(fn -> execute(initial) end)
Process.register(process, Storage)
process
end
def execute(state) do
receive do
{:get, sender} ->
send(sender, state)
execute(state)
{:put, val} ->
execute(state ++ [val])
end
end
def put(value) do
send(Storage, {:put, value})
end
def get do
send(Storage, {:get, self()})
receive do
msg -> msg
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment