Data.Stack
module Data
A last-in-first-out stack.
Opt-in: nothing here is in scope until using Data.Stack.
using Data.Stack
Elements are stored top first, so push, pop and peek are all list-head operations: none of them pay for the size of the stack. items reverses that internal order, so it reads bottom-to-top, the order you would have pushed them in:
let s = Stack.from([1, 2, 3])
s.peek # => Just(3)
s.push(4).items # => [1, 2, 3, 4]
s.pop # => Just((3, Stack(1, 2)))
Stack.empty.pop # => None
Every method answers with a new stack rather than changing the receiver. push! and pop! come free from the ! rebinding form, the same as add!/delete! do for Data.Set: they build a new stack and rebind the receiver variable rather than modifying anything in place.
record Stack<A>
A stack of elements, held top first.
Build one with Stack.from rather than by hand: the record literal takes elements in storage order (top first), which reads backwards from the items a caller normally thinks in.
Stack.from([1, 2, 3]).items # => [1, 2, 3]
Fields
elements[A]optional
module Data.Stack
Constructors for Stack.
function from
Builds a stack from a list, read bottom-to-top: the last element is on top.
from(items)
Parameters
items[A]- the elements, bottom first
Returns: Stack<A> — the stack, with items's last element on top
Examples
Stack.from([1, 2, 3]).peek # => Just(3)
constant empty Stack<A>
The stack with no elements. Also the Monoid identity.
make Stack<A> implements Enumerable, Foldable, Monoid, Showable
reduce
Folds from the top down to the bottom.
This is Stack's Enumerable/Foldable primitive.
reduce(acc, f) : B -> (B -> A -> B) -> B
Parameters
accB- the initial accumulator
fB -> A -> B- combines the accumulator with each element
Returns: B — the final accumulator
Examples
Stack.from([1, 2, 3]).reduce(0) { |sum, x| sum + x } # => 6
combine
Combines two stacks by pushing the argument's elements on top of this one, top element last.
combine(other) : Stack<A> -> Stack<A>
Parameters
otherStack<A>- the stack to push on top
Returns: Stack<A> — this stack with other stacked above it
Examples
Stack.from([1, 2]).combine(Stack.from([3, 4])).items # => [1, 2, 3, 4]
push
Returns a new stack with value pushed on top.
Use push! to rebind the receiver variable.
push(value) : A -> Stack<A>
Parameters
valueA- the element to push
Returns: Stack<A> — a stack with value on top
Examples
Stack.from([1, 2]).push(3).items # => [1, 2, 3]
pop
Returns the top element and the stack without it, wrapped in Just, or None for an empty stack.
Use pop! to rebind the receiver variable.
pop : (A, Stack<A>)?
Returns: (A, Stack<A>)? — the top element and the rest, or None
Examples
Stack.from([1, 2, 3]).pop # => Just((3, Stack(1, 2)))
Stack.empty.pop # => None
peek
Returns the top element wrapped in Just, or None for an empty stack.
peek : A?
Returns: A? — the top element, or None
Examples
Stack.from([1, 2, 3]).peek # => Just(3)
Stack.empty.peek # => None
+
Pushes another stack's elements, or a plain list's, on top.
The list form reads bottom-to-top, the same as Stack.from: the last element of the list ends up on top.
+(other) : Stack<A> -> Stack<A>
+(other) : [A] -> Stack<A>
Parameters
otherStack<A> | [A]- the elements to push
Returns: Stack<A> — this stack with other pushed above it
Examples
Stack.from([1, 2]) ` [3, 4] # => Stack(1, 2, 3, 4)
Stack.from([1, 2]) ` Stack.from([3]) # => Stack(1, 2, 3)