Create a gist now

Instantly share code, notes, and snippets.

@samdvr /agent.ex Secret
Created Jul 9, 2017

What would you like to do?
Agent
defmodule BankAccount do
def start_link(balance) do
Agent.start_link(fn -> balance end)
end
def deposit(account, amount) do
Agent.update(account, &(&1 + amount))
end
def withdraw(account, amount) do
Agent.update(account, &(&1 - amount))
end
def balance(account) do
Agent.get(account, &(&1))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment