Skip to content

Instantly share code, notes, and snippets.

@MonkeyIsNull
Created June 15, 2015 21:00
Show Gist options
  • Save MonkeyIsNull/0c6d056fc163694ea70d to your computer and use it in GitHub Desktop.
Save MonkeyIsNull/0c6d056fc163694ea70d to your computer and use it in GitHub Desktop.
Cart Example
defmodule Cart do
def start() do
{:ok, cart} = Agent.start(fn -> ["Seconds - by Bryan Lee O'Malley"] end)
cart
end
def print_cart(items) do
Enum.each(items, fn item -> IO.puts "Item: #{item}" end)
end
def show(cart) do
Agent.get(cart, fn(items) -> print_cart(items) end)
end
def add_item(cart, item) do
Agent.update(cart, fn(items) -> [item | items] end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment