Skip to content

Instantly share code, notes, and snippets.

@vortec
Created February 5, 2017 23:00
Show Gist options
  • Save vortec/d9de1b2d394f90c82f1230727a583311 to your computer and use it in GitHub Desktop.
Save vortec/d9de1b2d394f90c82f1230727a583311 to your computer and use it in GitHub Desktop.
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
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