{-# LANGUAGE FlexibleInstances, Safe #-}
module Data.Chatty.None where
import Data.Maybe
import Data.Text (pack, Text)
class None n where
none :: n
instance None [a] where
none :: [a]
none = []
instance None (Maybe a) where
none :: Maybe a
none = Maybe a
forall a. Maybe a
Nothing
instance None () where
none :: ()
none = ()
instance None Text where
none :: Text
none = String -> Text
pack String
""
instance None (a -> a) where
none :: a -> a
none = a -> a
forall a. a -> a
id
instance Monad m => None (a -> m a) where
none :: a -> m a
none = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
instance None Int where
none :: Int
none = Int
0
instance None Integer where
none :: Integer
none = Integer
0
instance None Bool where
none :: Bool
none = Bool
False
noneM :: (Monad m,None n) => m n
noneM :: m n
noneM = n -> m n
forall (m :: * -> *) a. Monad m => a -> m a
return n
forall n. None n => n
none
joinMaybe :: None n => Maybe n -> n
joinMaybe :: Maybe n -> n
joinMaybe (Just n
j) = n
j
joinMaybe Maybe n
Nothing = n
forall n. None n => n
none
expandMaybe :: (Eq n,None n) => n -> Maybe n
expandMaybe :: n -> Maybe n
expandMaybe n
n
| n
n n -> n -> Bool
forall a. Eq a => a -> a -> Bool
== n
forall n. None n => n
none = Maybe n
forall a. Maybe a
Nothing
| Bool
otherwise = n -> Maybe n
forall a. a -> Maybe a
Just n
n
cleanMaybe :: (Eq n,None n) => Maybe n -> Maybe n
cleanMaybe :: Maybe n -> Maybe n
cleanMaybe = n -> Maybe n
forall n. (Eq n, None n) => n -> Maybe n
expandMaybe (n -> Maybe n) -> (Maybe n -> n) -> Maybe n -> Maybe n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe n -> n
forall n. None n => Maybe n -> n
joinMaybe
reduce :: (Eq n,None n) => [n] -> [n]
reduce :: [n] -> [n]
reduce = (n -> Bool) -> [n] -> [n]
forall a. (a -> Bool) -> [a] -> [a]
filter (n -> n -> Bool
forall a. Eq a => a -> a -> Bool
/=n
forall n. None n => n
none)
reduceMaybe :: [Maybe a] -> [a]
reduceMaybe :: [Maybe a] -> [a]
reduceMaybe = (Maybe a -> a) -> [Maybe a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Maybe a -> a
forall a. Maybe a -> a
unjust ([Maybe a] -> [a]) -> ([Maybe a] -> [Maybe a]) -> [Maybe a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> Bool) -> [Maybe a] -> [Maybe a]
forall a. (a -> Bool) -> [a] -> [a]
filter Maybe a -> Bool
forall a. Maybe a -> Bool
isJust
where unjust :: Maybe a -> a
unjust (Just a
j) = a
j