-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.exs
37 lines (32 loc) · 884 Bytes
/
bucket.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defmodule Bucket do
#This file is in the root folder since, I ran it on iex
#This is done by running c "bucket.exs" then press enter. Boom, you can
#Start your server and interact with it, run basic things
use Agent, restart: :temporary
@doc """
Start a new bucket
"""
#@spec start_link(opts)
def start_link(opts) do
Agent.start_link(fn -> %{} end)
end
@doc """
Takes 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
@doc """
Deletes `key` from `bucket`
Returns the current value of `key` if `key` exists
"""
def delete(bucket, key) do
Agent.get_and_update(bucket, fn dict -> Map.pop(dict,key) end)
end
end