Stack
A stack is a last-in-first-out (LIFO) data structure object.
Values are added and removed from the same end (the "top") via push and pop, so the most recently pushed value is always the first one popped.
The stack can also be iterated destructively with iterator, which pops values one by one until it is empty.
Constructor
Stack(): StackFields
| Name | Type | Description |
|---|---|---|
size | integer | The size of the stack. Read-only |
Methods
clear | Empties the stack, removing every value and resetting its size to 0. |
filter | Removes every item for which predicate returns true, keeping the order of the remaining items. |
isEmpty | Checks if the stack is empty. |
iterator | Returns an iterator for the stack, suitable for use in a for loop (e.g. for value in stack:iterator() do ... end). |
peek | Returns the value at the top of the stack. |
pop | Pops the value from the top of the stack. |
push | Pushes a value onto the stack. |
#Stack:clear
function Stack:clear()
Empties the stack, removing every value and resetting its size to 0.
#Stack:filter
function Stack:filter(predicate: fun( item: T ): boolean)
Removes every item for which predicate returns true, keeping the order of the remaining items.
| Name | Type | Description |
|---|---|---|
predicate | fun( item: T ): boolean | The function that decides whether a frame is removed. |
#Stack:isEmpty
function Stack:isEmpty(): boolean
Checks if the stack is empty.
| Type | Name | Description |
|---|---|---|
boolean | is_empty | Returns true if the stack has no values, otherwise false. |
#Stack:iterator
function Stack:iterator(): (fun( self: Stack<T> ): T | nil), Stack<T>
Returns an iterator for the stack, suitable for use in a for loop (e.g. for value in stack:iterator() do ... end). Iterating pops values off the stack one at a time until it is empty, so this consumes the stack.
#Stack:peek
function Stack:peek(): T | nil
Returns the value at the top of the stack.
| Type | Name | Description |
|---|---|---|
T | nil | value | The value at the top of the stack, or nil if the stack is empty. |
#Stack:pop
function Stack:pop(): T | nil
Pops the value from the top of the stack.
| Type | Name | Description |
|---|---|---|
T | nil | value | The value that was removed from the stack, or nil if the stack is empty. |
#Stack:push
function Stack:push(value: T): integer
Pushes a value onto the stack.
| Name | Type | Description |
|---|---|---|
value | T | The value to push onto the stack. |
| Type | Name | Description |
|---|---|---|
integer | position | The position of the value in the stack. |