module Matterhorn.Types.NonemptyStack
( NonemptyStack
, newStack
, push
, pop
, top
, stackToList
)
where
import Prelude ()
import Matterhorn.Prelude
data NonemptyStack a =
NonemptyStack { forall a. NonemptyStack a -> a
bottom :: !a
, forall a. NonemptyStack a -> [a]
rest :: ![a]
}
deriving (Int -> NonemptyStack a -> ShowS
[NonemptyStack a] -> ShowS
NonemptyStack a -> String
(Int -> NonemptyStack a -> ShowS)
-> (NonemptyStack a -> String)
-> ([NonemptyStack a] -> ShowS)
-> Show (NonemptyStack a)
forall a. Show a => Int -> NonemptyStack a -> ShowS
forall a. Show a => [NonemptyStack a] -> ShowS
forall a. Show a => NonemptyStack a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> NonemptyStack a -> ShowS
showsPrec :: Int -> NonemptyStack a -> ShowS
$cshow :: forall a. Show a => NonemptyStack a -> String
show :: NonemptyStack a -> String
$cshowList :: forall a. Show a => [NonemptyStack a] -> ShowS
showList :: [NonemptyStack a] -> ShowS
Show)
newStack :: a -> NonemptyStack a
newStack :: forall a. a -> NonemptyStack a
newStack a
v = NonemptyStack { bottom :: a
bottom = a
v
, rest :: [a]
rest = []
}
stackToList :: NonemptyStack a -> [a]
stackToList :: forall a. NonemptyStack a -> [a]
stackToList (NonemptyStack a
b [a]
as) = [a]
as [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a
b]
pop :: NonemptyStack a -> (NonemptyStack a, Maybe a)
pop :: forall a. NonemptyStack a -> (NonemptyStack a, Maybe a)
pop s :: NonemptyStack a
s@(NonemptyStack a
_ []) = (NonemptyStack a
s, Maybe a
forall a. Maybe a
Nothing)
pop s :: NonemptyStack a
s@(NonemptyStack a
_ (a
a:[a]
as)) = (NonemptyStack a
s { rest = as }, a -> Maybe a
forall a. a -> Maybe a
Just a
a)
push :: a -> NonemptyStack a -> NonemptyStack a
push :: forall a. a -> NonemptyStack a -> NonemptyStack a
push a
v NonemptyStack a
s = NonemptyStack a
s { rest = v : rest s }
top :: NonemptyStack a -> a
top :: forall a. NonemptyStack a -> a
top (NonemptyStack a
_ (a
a:[a]
_)) = a
a
top (NonemptyStack a
bot []) = a
bot