Postfix.Stack (postfix v1.1.334)

Evaluation of terms using postfix order and a stack

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}

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.

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