Created
February 5, 2017 22:18
-
-
Save vortec/c398a347e47cdd0af461b6cfae15fa17 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 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 |
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
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