Created
February 5, 2017 23:00
-
-
Save vortec/d9de1b2d394f90c82f1230727a583311 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.Bucket do | |
@doc """ | |
Starts a new bucket. | |
""" | |
def start_link do | |
Agent.start_link(fn -> %{} end) | |
end | |
@doc """ | |
Gets a value from the `bucket` by `key`. | |
""" | |
def get(bucket, key) do | |
Agent.get(bucket, fn b -> Map.get(b, key) end) | |
end | |
@doc """ | |
Puts the `value` for the given `key` in the `bucket`. | |
""" | |
def put(bucket, key, value) do | |
Agent.update(bucket, fn b -> Map.put(b, 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
defmodule KV.Bucket do | |
@doc """ | |
Starts a new bucket. | |
""" | |
def start_link do | |
Agent.start_link(fn -> %{} end) | |
end | |
@doc """ | |
Gets a value from the `bucket` by `key`. | |
""" | |
def get(bucket, key) do | |
Agent.get(bucket, &Map.get(&1, key)) | |
end | |
@doc """ | |
Puts the `value` for the given `key` in the `bucket`. | |
""" | |
def put(bucket, key, value) do | |
Agent.update(bucket, &Map.put(&1, key, value)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment