Skip to content

Instantly share code, notes, and snippets.

@MonkeyIsNull
Created November 6, 2015 12:46
Show Gist options
  • Save MonkeyIsNull/a5b97b52117a97ca5742 to your computer and use it in GitHub Desktop.
Save MonkeyIsNull/a5b97b52117a97ca5742 to your computer and use it in GitHub Desktop.
defmodule LlCatStore do
def loop(cats) do
receive do
{:add, cat} ->
IO.puts "adding cat #{cat}"
loop([cat | cats])
{:remove, cat} ->
IO.puts "removing cat #{cat}"
loop(remove_cat(cat, cats))
{:list} ->
Enum.each(cats, fn(c) -> IO.puts("Cat: #{c}") end)
loop(cats)
end
end
def remove_cat(cat, current_cats) do
case cat in current_cats do
true ->
List.delete(current_cats, cat)
false ->
current_cats
end
end
def start() do
spawn(fn -> loop(["BigglesWorth"]) end)
end
end
#iex(1)> foo = LlCatStore.start
##PID<0.94.0>
#iex(2)> send(foo, {:add, "Fuzzy"})
#adding cat Fuzzy
#{:add, "Fuzzy"}
#iex(3)> send(foo, {:add, "LuLu"})
#adding cat LuLu
#{:add, "LuLu"}
#iex(4)> send(foo, {:add, "JohnnyQuest"})
#adding cat JohnnyQuest
#{:add, "JohnnyQuest"}
#iex(5)> send(foo, {:remove, "JohnnyQuest"})
#removing cat JohnnyQuest
#{:remove, "JohnnyQuest"}
#iex(6)> send(foo, {:list})
#Cat: LuLu
#Cat: Fuzzy
#Cat: BigglesWorth
#{:list}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment