Skip to content

Instantly share code, notes, and snippets.

@vortec
Created February 5, 2017 22:18
Show Gist options
  • Save vortec/c398a347e47cdd0af461b6cfae15fa17 to your computer and use it in GitHub Desktop.
Save vortec/c398a347e47cdd0af461b6cfae15fa17 to your computer and use it in GitHub Desktop.
defmodule KV do
def start_link do
Task.start_link(fn -> loop(%{}) end)
end
defp loop(map) do
receive do
{:get, key, caller} ->
send caller, Map.get(map, key)
loop(map)
{:put, key, value} ->
loop(Map.put(map, key, value))
end
end
end
my_pid = self()
{:ok, pid} = KV.start_link
send pid, {:put, :hello, "james"}
send pid, {:get, :hello, my_pid}
receive do
value ->
IO.puts(value) # => james
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment