From b155dd013889025907a96d2131504a5c9a4dc5bc Mon Sep 17 00:00:00 2001 From: Michael Dimmitt Date: Fri, 12 Oct 2018 00:03:35 -0400 Subject: [PATCH 1/6] give the recieve function some impatience --- lib/stack.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/stack.ex b/lib/stack.ex index 5fa9ac9..196b518 100644 --- a/lib/stack.ex +++ b/lib/stack.ex @@ -13,6 +13,8 @@ defmodule Stack do receive do {:pop, val} -> val + after + 1000 -> :ok end end From e213a9544aede1fe29467dba85fd2a8666a6c57c Mon Sep 17 00:00:00 2001 From: Michael Dimmitt Date: Fri, 12 Oct 2018 00:24:42 -0400 Subject: [PATCH 2/6] Add a conversation with an audience_member and transcript that return a stack. --- README.md | 13 +++++++++++++ lib/conversation.ex | 23 +++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 59cefaa..248b9ea 100644 --- a/README.md +++ b/README.md @@ -21,5 +21,18 @@ 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) +``` diff --git a/lib/conversation.ex b/lib/conversation.ex index 6bc66f4..c3feee2 100644 --- a/lib/conversation.ex +++ b/lib/conversation.ex @@ -3,16 +3,23 @@ defmodule Conversation do Documentation for Conversation. """ - @doc """ - Hello world. + def new do + stack = Conversation.audience_member + Conversation.transcript(stack) + Conversation.transcript(stack) + stack + end - ## Examples + def audience_member do + Stack.new + end - iex> Conversation.hello - :world + def transcript (stack) do + Stack.push(stack, :foo) + Stack.push(stack, :bar) + end - """ - def hello do - :world + def pop(stack) do + Stack.pop(stack) end end From 03cdb09767db419e675f53deb38cc86f42c984f9 Mon Sep 17 00:00:00 2001 From: Michael Dimmitt Date: Fri, 12 Oct 2018 00:26:41 -0400 Subject: [PATCH 3/6] do stuff in background function and doctests added --- README.md | 2 ++ lib/conversation.ex | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 248b9ea..42d6b3a 100644 --- a/README.md +++ b/README.md @@ -34,5 +34,7 @@ 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 c3feee2..ece8391 100644 --- a/lib/conversation.ex +++ b/lib/conversation.ex @@ -3,11 +3,42 @@ defmodule Conversation do Documentation for Conversation. """ + @doc """ + Do the stuff but dont tell me. + + ## Examples + iex> Conversation.iCanDoStuffAllTheStuffButYouNoSee + :ok + + 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 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 - stack = Conversation.audience_member - Conversation.transcript(stack) - Conversation.transcript(stack) - stack + person1 = Conversation.audience_member + Conversation.transcript(person1) + Conversation.transcript(person1) + Conversation.transcript(person1) + person1 end def audience_member do From 76d808ee3ac97f2533461c142ffef385c8f55930 Mon Sep 17 00:00:00 2001 From: Michael Dimmitt Date: Fri, 12 Oct 2018 22:15:47 -0400 Subject: [PATCH 4/6] peek and show methods can be called from Conversation or Stack --- lib/conversation.ex | 8 ++++++++ lib/stack.ex | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/conversation.ex b/lib/conversation.ex index ece8391..d49a987 100644 --- a/lib/conversation.ex +++ b/lib/conversation.ex @@ -45,6 +45,14 @@ defmodule Conversation 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) diff --git a/lib/stack.ex b/lib/stack.ex index 196b518..b6b7e51 100644 --- a/lib/stack.ex +++ b/lib/stack.ex @@ -7,6 +7,24 @@ defmodule Stack do send(pid, {:push, val}) 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 pop(pid) do send(pid, {:pop, self()}) val = send(pid, {:pop, self()}) @@ -22,7 +40,16 @@ defmodule Stack do def loop(state) do new_state = receive do + + {:peek, caller} -> + [head | _] = state + send(caller, {:peek, head}) + state + {:push, val} -> [val | state] + {:show, caller} -> + send(caller, {:pop, state}) + state {:pop, caller} -> [head | new_state] = state From 47721cd3a765e77cd4308020e8e09a36ae3bc06b Mon Sep 17 00:00:00 2001 From: Michael Dimmitt Date: Fri, 12 Oct 2018 22:23:41 -0400 Subject: [PATCH 5/6] cleanup structure and formatting. --- lib/stack.ex | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/stack.ex b/lib/stack.ex index b6b7e51..36d30c5 100644 --- a/lib/stack.ex +++ b/lib/stack.ex @@ -3,10 +3,6 @@ defmodule Stack do spawn(fn -> loop([]) end) end - def push(pid, val) do - send(pid, {:push, val}) - end - def peek(pid) do send(pid, {:peek, self()}) receive do @@ -25,16 +21,19 @@ defmodule Stack do 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 - 1000 -> :ok + 0 -> :ok end - end def loop(state) do @@ -46,11 +45,13 @@ defmodule Stack do send(caller, {:peek, head}) state - {:push, val} -> [val | state] {:show, caller} -> send(caller, {:pop, state}) state + {:push, val} -> + [val | state] + {:pop, caller} -> [head | new_state] = state send(caller, {:pop, head}) From 675f7901b917c869a97ba0c7e8b3a05eb7284a17 Mon Sep 17 00:00:00 2001 From: Michael Dimmitt Date: Fri, 12 Oct 2018 22:25:22 -0400 Subject: [PATCH 6/6] Use pattern matching to handle an empty array. --- lib/stack.ex | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/stack.ex b/lib/stack.ex index 36d30c5..dbed650 100644 --- a/lib/stack.ex +++ b/lib/stack.ex @@ -53,10 +53,21 @@ defmodule Stack do [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