module Data.ID
(ID
,split
,fromList
,singleton
,cons
,snoc)
where
import Data.Aeson
import Data.AttoLisp
import Data.Data
import Data.Default
import Data.List
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE
import Data.Semigroup
newtype ID =
ID (NonEmpty Integer)
deriving (Eq,Semigroup,Data,Typeable)
instance ToJSON ID where
toJSON (ID (x :| xs)) = toJSON (x : xs)
instance ToLisp ID where
toLisp (ID (x :| xs)) = toLisp (x : xs)
instance Default ID where
def = singleton 0
instance Show ID where
show (ID (x :| xs)) = "@" ++ intercalate "→" (map show (x : xs))
split :: ID -> (Integer,Maybe ID)
split (ID (x :| xs)) = (x,fromList xs)
fromList :: [Integer] -> Maybe ID
fromList = fmap ID . NE.nonEmpty
singleton :: Integer -> ID
singleton = ID . nesingleton
cons :: Integer -> [Integer] -> ID
cons x xs = ID (x :| xs)
snoc :: ID -> Integer -> ID
snoc (ID xs) i = ID (xs <> nesingleton i)
nesingleton :: a -> NonEmpty a
nesingleton = (:| [])