diff --git a/README.md b/README.md index 59cefaa..42d6b3a 100644 --- a/README.md +++ b/README.md @@ -21,5 +21,20 @@ git clone https://github.com/MichaelDimmitt/transcript-fromElixirWorkshop.git; cd transcript-fromElixirWorkshop; iex -S mix; ``` +#Usage Below +## bash +`iex -S mix` + +## iex syntax: +```elixir +stack = Conversation.new +Conversation.pop(stack) +Conversation.pop(stack) +Conversation.pop(stack) +Conversation.pop(stack) +Conversation.pop(stack) + +Conversation.iCanDoStuffAllTheStuffButYouNoSee +``` diff --git a/lib/conversation.ex b/lib/conversation.ex index 6bc66f4..d49a987 100644 --- a/lib/conversation.ex +++ b/lib/conversation.ex @@ -4,15 +4,61 @@ defmodule Conversation do """ @doc """ - Hello world. + Do the stuff but dont tell me. ## Examples + iex> Conversation.iCanDoStuffAllTheStuffButYouNoSee + :ok - iex> Conversation.hello - :world + Tell me the stuff you are doing. + ## Examples + iex> stack = Conversation.new + iex> Conversation.pop(stack) + iex> Conversation.pop(stack) + iex> Conversation.pop(stack) + iex> Conversation.pop(stack) + iex> Conversation.pop(stack) + :ok """ - def hello do - :world + + def iCanDoStuffAllTheStuffButYouNoSee do + stack = Conversation.new + Conversation.pop(stack) + Conversation.pop(stack) + Conversation.pop(stack) + Conversation.pop(stack) + Conversation.pop(stack) + Conversation.pop(stack) + Conversation.pop(stack) + end + + def new do + person1 = Conversation.audience_member + Conversation.transcript(person1) + Conversation.transcript(person1) + Conversation.transcript(person1) + person1 + end + + def audience_member do + Stack.new + end + + def show(pid) do + Stack.show(pid) + end + + def peek(pid) do + Stack.peek(pid) + end + + def transcript (stack) do + Stack.push(stack, :foo) + Stack.push(stack, :bar) + end + + def pop(stack) do + Stack.pop(stack) end end diff --git a/lib/stack.ex b/lib/stack.ex index 5fa9ac9..dbed650 100644 --- a/lib/stack.ex +++ b/lib/stack.ex @@ -3,30 +3,71 @@ defmodule Stack do spawn(fn -> loop([]) end) end + def peek(pid) do + send(pid, {:peek, self()}) + receive do + {:peek, val} -> val + after + 0 -> :ok + end + end + + def show(pid) do + send(pid, {:show, self()}) + receive do + {:pop, val} -> val + after + 0 -> :ok + end + end + def push(pid, val) do send(pid, {:push, val}) end def pop(pid) do send(pid, {:pop, self()}) - val = send(pid, {:pop, self()}) - receive do - {:pop, val} -> val + val -> + IO.puts "Resulting variable: #{ inspect val } ." + val + after + 0 -> :ok end - end def loop(state) do new_state = receive do - {:push, val} -> [val | state] + + {:peek, caller} -> + [head | _] = state + send(caller, {:peek, head}) + state + + {:show, caller} -> + send(caller, {:pop, state}) + state + + {:push, val} -> + [val | state] {:pop, caller} -> - [head | new_state] = state - send(caller, {:pop, head}) - new_state + check(state, caller) + end loop(new_state) end + + def check([], caller) do + send(caller, []) + [] + end + + def check(state, caller) do + IO.puts "current state: #{ inspect state }" + [head | new_state] = state + send(caller, head) + new_state + end end