{-# LANGUAGE DeriveLift #-}
module Burrito.Type.NonEmpty
( NonEmpty(..)
, fromList
, singleton
, toList
)
where
import qualified Language.Haskell.TH.Syntax as TH
data NonEmpty a = NonEmpty
{ first :: a
, rest :: [a]
} deriving (Eq, TH.Lift, Show)
fromList :: [a] -> Maybe (NonEmpty a)
fromList xs = case xs of
[] -> Nothing
x : ys -> Just NonEmpty { first = x, rest = ys }
singleton :: a -> NonEmpty a
singleton x = NonEmpty { first = x, rest = [] }
toList :: NonEmpty a -> [a]
toList xs = first xs : rest xs