Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern match handle empty array error #7

Open
wants to merge 6 commits into
base: dev-with-tests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

56 changes: 51 additions & 5 deletions lib/conversation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 49 additions & 8 deletions lib/stack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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