{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Distribution.Utils.NubList
( NubList
, toNubList
, fromNubList
, overNubList
, NubListR
, toNubListR
, fromNubListR
, overNubListR
) where
import Distribution.Compat.Prelude
import Prelude ()
import Distribution.Simple.Utils
import qualified Text.Read as R
newtype NubList a =
NubList { fromNubList :: [a] }
deriving (Eq, Generic, Typeable)
toNubList :: Ord a => [a] -> NubList a
toNubList list = NubList $ ordNub list
overNubList :: Ord a => ([a] -> [a]) -> NubList a -> NubList a
overNubList f (NubList list) = toNubList . f $ list
instance Ord a => Monoid (NubList a) where
mempty = NubList []
mappend = (<>)
instance Ord a => Semigroup (NubList a) where
(NubList xs) <> (NubList ys) = NubList $ xs `listUnion` ys
instance Show a => Show (NubList a) where
show (NubList list) = show list
instance (Ord a, Read a) => Read (NubList a) where
readPrec = readNubList toNubList
readNubList :: (Read a) => ([a] -> l a) -> R.ReadPrec (l a)
readNubList listToL = R.parens . R.prec 10 $ fmap listToL R.readPrec
instance (Ord a, Binary a) => Binary (NubList a) where
put (NubList l) = put l
get = fmap toNubList get
instance Structured a => Structured (NubList a)
newtype NubListR a =
NubListR { fromNubListR :: [a] }
deriving Eq
toNubListR :: Ord a => [a] -> NubListR a
toNubListR list = NubListR $ ordNubRight list
overNubListR :: Ord a => ([a] -> [a]) -> NubListR a -> NubListR a
overNubListR f (NubListR list) = toNubListR . f $ list
instance Ord a => Monoid (NubListR a) where
mempty = NubListR []
mappend = (<>)
instance Ord a => Semigroup (NubListR a) where
(NubListR xs) <> (NubListR ys) = NubListR $ xs `listUnionRight` ys
instance Show a => Show (NubListR a) where
show (NubListR list) = show list
instance (Ord a, Read a) => Read (NubListR a) where
readPrec = readNubList toNubListR