A basic implementaion of a generic Stack data strucutre.
Given a Stack, returns True if the Stack is empty
or False if Stack has one or more elements.
Stack.isEmpty Stack.empty => True
List.range 1 10
|> Stack.fromList
|> Stack.isEmpty => FalseReturns how many elements are in the Stack as an Int.
Stack.size Stack.empty => 0
Stack.fromList [1, 2, 3]
|> Stack.size => 3Given a Stack, returns a list where the head of the list
is the first element on top of the stack.
Stack.toList Stack.empty => []
Stack.empty
|> Stack.push "foo"
|> Stack.push "bar"
|> Stack.toList => ["bar", "foo"]Given a list, returns a Stack where the top of the stack
is the head of the list.
Stack.fromList [] => Stack []
Stack.fromList [1, 2, 3] => Stack 3 [1, 2, 3]Given a Stack, returns a Tuple.
The first element is Just the element on top of
the stack or Nothing if the stack is empty.
The second element is a new Stack without the top of the stack
or an empty Stack if the given Stack was empty.
Stack.pop Stack.empty => (Nothing, Stack.empty)
Stack.fromList [1, 2, 3]
|> Stack.pop => (Just 1, Stack 2 [2, 3])Given a value a and a Stack a, push the element onto
the top of the Stack.
Stack.push "foo" Stack.empty => Stack 1 ["foo"]
Stack.fromList ["foo", "bar"]
|> Stack.push "bazz" => Stack 3 ["bazz", "foo", "bar"]Given a Stack, return Just the top of the Stack or
Nothing if the Stack is empty.
Stack.peek Stack.empty => Nothing
Stack.fromList [1, 2, 3]
|> Stack.peek => Just 1Given a value a and a Stack a, check if the value is in the
Stack returning True if it is and False if it isn't.
Stack.member "foobar" Stack.empty => False
Stack.fromList ["Mon", "Tues", "Wed", "Thurs", "Fri"]
|> Stack.member "Thurs" => TrueGiven two Stacks, create a new Stack where the second stack is
appended onto the end of the first Stack.
stack1 = Stack.fromList [1, 2]
stack2 = Stack.fromList [3, 4]
Stack.append stack1 stack2 => Stack [1, 2, 3, 4]Given a list of stacks, join them together where the top of the first
Stack in the given list is the top of the returned Stack.
stacks =
[ Stack.fromList [1, 2]
, Stack.fromList [3, 4]
, Stack.fromList [5, 6]
]
Stack.concat stacks => Stack 6 [1, 2, 3, 4, 5, 6]Given a function (a -> b) and a Stack a return a new
Stack b with the function applied to every element of the stack. Similar to List.map
Stack.fromList [1, 2, 3]
|> Stack.map ((+) 1) => Stack 3 [2, 3, 4]
Stack.fromList [1, 2, 3]
|> Stack.map String.fromInt => Stack 3 ["1", "2", "3"]
A generic stack definition.