Created
June 1, 2020 20:22
-
-
Save josefrichter/0a3ccb42c9a4098eb1dc7c29d231be15 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 MovieData do | |
def start_link do | |
Agent.start_link(fn -> %{} end) | |
end | |
def add(pid, movie) do | |
Agent.update(pid, fn(state) -> | |
Map.put(state, movie, 1) | |
end) | |
end | |
def reset(pid) do | |
Agent.update(pid, fn(_state) -> %{} end) | |
end | |
def watch_count(pid, movie) do | |
Agent.get(pid, fn(state) -> | |
Map.get(state, movie) | |
end) | |
end | |
def watch(pid, movie) do | |
Agent.update(pid, fn(state) -> | |
count = Map.get(state, movie) | |
Map.replace(state, movie, count + 1) | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment