Postfix.Stack (postfix v1.2.339)

Evaluation of terms using postfix order and a stack.

Shuffle words can manipulate the stack and are represented as atoms aliased under this namespace using the Factor vocabulary:

  • Dup - duplicate the item at the top of the stack

Create the aliases with:

alias Postfix.Stack.{Dup, ...}

Link to this section Summary

Functions

Evaluate a list of values and functions using a stack.

Link to this section Functions

@spec eval([term()]) :: {:ok, term()} | {:error, any()}

Evaluate a list of values and functions using a stack.

iex> Postfix.Stack.eval([2, 3, &*/2, 4, &+/2])
{:ok, 10}

iex> Postfix.Stack.eval([1, 2, &-/2])
{:ok, -1}

iex> Postfix.Stack.eval([1, 0, &div/2])
{:error, %ArithmeticError{}}

Values are pushed on to the stack.

Functions are defined by their arity and there must be sufficient operands available on the stack otheriwse Postfix.Stack.StackError is raised. Operands are given to the function in fifo order (left-to-right).

The return value of any function is pushed onto the stack. If the function returns an {:ok, value} tuple then the value is unwrapped.

If any function returns an {:error, error} tuple then the evaluation is short-circuited and the error tuple is returned. Errors are rescued and returned as {:error, %Error{}} tuples.

Returns the last value on the stack within an {:ok, ...} tuple, or {:ok, nil} if the stack is empty.