Skip to content

Instantly share code, notes, and snippets.

@josefrichter
Created June 1, 2020 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefrichter/0a3ccb42c9a4098eb1dc7c29d231be15 to your computer and use it in GitHub Desktop.
Save josefrichter/0a3ccb42c9a4098eb1dc7c29d231be15 to your computer and use it in GitHub Desktop.
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