module Data.Apart.Structures.Stack
(Stack, insert, singleton, foldaway, final) where
import Control.Comonad.Cofree (Cofree (..), unwrap)
import Data.Functor.Contravariant (Predicate (..))
import Data.Apart.Apart (Segment (..))
type Stack = Cofree Maybe
singleton :: a -> Stack a
singleton x = x :< Nothing
insert :: a -> Stack a -> Stack a
insert x = (:<) x . Just
foldaway :: Foldable t => t a -> Segment Stack a
foldaway = foldr (\el -> Just . (:<) el) Nothing
final :: Eq a => Predicate (Stack a)
final = Predicate $ \s -> unwrap s == Nothing