Ergo.Stack (Ergo v0.4.5)

Context user-data represents a stack rather than a map or plain list, so we'll create an interface that is more stack like than the list interface.

Link to this section Summary

Functions

Create a new stack.

Get the top element of the stack without modifying the stack

You cannot pop an element from an empty stack.

Push an element onto an empty stack

Link to this section Functions

Create a new stack.

Examples

iex> alias Ergo.Stack
iex> assert [] = Stack.new()

Get the top element of the stack without modifying the stack

Examples

iex> alias Ergo.Stack
iex> s = Stack.new() |> Stack.push(1) |> Stack.push(2)
iex> assert 2 = Stack.peek(s)

You cannot pop an element from an empty stack.

At this point it's not clear if this operation should raise an exception or return an error. For now we'll return an error.

Examples

iex> alias Ergo.Stack
iex> s = Stack.new()
iex> assert {:error, nil, []} = Stack.pop(s)
iex> s = s |> Stack.push(true) |> Stack.push(false)
iex> assert [false, true] = s
iex> assert {:ok, false, [true]} = Stack.pop(s)

Push an element onto an empty stack

Examples

iex> alias Ergo.Stack
iex> s = Stack.new()
iex> assert [1] = Stack.push(s, 1)
iex> assert [2, 1] = Stack.push(s, 1) |> Stack.push(2)