raw-feldspar-0.4.1: src/Data/Selection.hs
{-# LANGUAGE CPP #-}
-- | Data structure for describing selections of values
module Data.Selection
( Selection
, includes
, selectBy
, empty
, universal
, select
, union
, intersection
, difference
, allExcept
) where
-- | Selection: description of a set of values
data Selection a = Selection (a -> Bool)
#if MIN_VERSION_base(4,11,0)
instance Semigroup (Selection a)
where
(<>) = union
#endif
-- |
-- @
-- `mempty` = `empty`
-- `mappend` = `union`
-- @
instance Monoid (Selection a)
where
mempty = empty
mappend = (<>)
-- | Check whether a value is included in a selection
includes :: Selection a -> a -> Bool
includes (Selection p) = p
-- | Select the values that fulfill a predicate
selectBy :: (a -> Bool) -> Selection a
selectBy = Selection
-- | Empty selection
empty :: Selection a
empty = Selection $ \_ -> False
-- | Select all values
universal :: Selection a
universal = Selection $ \_ -> True
-- | Union of selections
union :: Selection a -> Selection a -> Selection a
union s t = Selection $ \a -> includes s a || includes t a
-- | Intersection of selections
intersection :: Selection a -> Selection a -> Selection a
intersection s t = Selection $ \a -> includes s a && includes t a
-- | Difference of selections
difference :: Selection a -> Selection a -> Selection a
difference s t = Selection $ \a -> includes s a && not (includes t a)
-- | Create a classification from a list of elements
select :: Eq a => [a] -> Selection a
select as = selectBy (`elem` as)
-- | Select all values except those in the given list
allExcept :: Eq a => [a] -> Selection a
allExcept = difference universal . select