Created
April 25, 2016 20:42
-
-
Save MonkeyIsNull/1549123f9b2c519ba3bfc4ec7d59ace4 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 Catstore do | |
def start() do | |
{:ok, store} = Agent.start(fn -> ["Biggles"] end) | |
store | |
end | |
def pr_cats(cats) do | |
Enum.each(cats, fn cat -> IO.puts cat end) | |
end | |
def show_cats(store) do | |
Agent.get(store, fn(cats) -> pr_cats(cats) end) | |
end | |
def seek_cat([],_) do | |
end | |
def seek_cat([h|t], which_cat) do | |
cond do | |
h == which_cat -> | |
h | |
true -> | |
seek_cat(t, which_cat) | |
end | |
end | |
def find_cat(store, which_cat) do | |
Agent.get(store, fn(cats) -> seek_cat(cats, which_cat) end) | |
end | |
def add_cat(store, cat) do | |
Agent.update(store, fn(cats) -> [ cat | cats] end) | |
end | |
end | |
c = Catstore.start() | |
Catstore.show_cats(c) | |
Catstore.add_cat(c, "Lulu") | |
Catstore.show_cats(c) | |
w = Catstore.find_cat(c, "Lulu") | |
IO.puts "Cat #{w}" | |
no_cat = Catstore.find_cat(c, "Zany") | |
# Not one here | |
IO.puts "Cat #{no_cat}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment