Created
November 17, 2015 17:20
-
-
Save gausby/01b999a495d834d22722 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Counter do | |
def start_link do | |
Agent.start_link(fn -> 0 end) | |
end | |
def set(pid, value) do | |
Agent.update(pid, fn _ -> value end) | |
end | |
def increment(pid) do | |
Agent.get_and_update(pid, fn state -> | |
{state + 1, state + 1} | |
end) | |
end | |
def decrement(pid) do | |
Agent.get_and_update(pid, fn state -> | |
{state - 1, state - 1} | |
end) | |
end | |
end | |
{:ok, pid} = Counter.start_link | |
Counter.increment(pid) | |
Counter.increment(pid) | |
Counter.increment(pid) | |
Counter.set(pid, 5) | |
Counter.decrement(pid) | |
Counter.increment(pid) | |
Agent.get(pid, &(&1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment