{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Internal.SnocList
( SnocList
, fromList
, empty
, snoc
, singleton
) where
import Data.Foldable
import Data.Hashable (Hashable)
newtype SnocList a = SnocList [a]
deriving(Eq, Hashable)
fromList :: [a] -> SnocList a
fromList = SnocList . reverse
singleton :: a -> SnocList a
singleton x = SnocList [x]
empty :: SnocList a
empty = SnocList []
snoc :: SnocList a -> a -> SnocList a
snoc (SnocList xs) x = SnocList (x:xs)
instance Foldable SnocList where
foldMap f = foldMap f . toList
toList (SnocList xs) = reverse xs
instance Semigroup (SnocList a) where
(SnocList l) <> (SnocList r) = SnocList (r <> l)
instance Monoid (SnocList a) where
mempty = empty