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

Peek and show functions #8

Open
wants to merge 9 commits into
base: dev-with-tests
Choose a base branch
from
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
@doc """
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)
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
40 changes: 35 additions & 5 deletions lib/stack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,54 @@ 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
Expand Down