module Data.Invertible.Maybe
( isJust
, isNothing
, listToMaybe
, maybeToList
, fromMaybe
) where
import qualified Data.Maybe as M
import Data.Invertible.Bijection
import Data.Invertible.TH
import Data.Invertible.Internal
isJust :: Maybe () <-> Bool
isJust =
[biCase|
Just () <-> True
Nothing <-> False
|]
isNothing :: Maybe () <-> Bool
isNothing =
[biCase|
Nothing <-> True
Just () <-> False
|]
listToMaybe :: [a] <-> Maybe a
listToMaybe = M.listToMaybe :<->: M.maybeToList
maybeToList :: Maybe a <-> [a]
maybeToList = invert listToMaybe
fromMaybe :: Eq a => a -> Maybe a <-> a
fromMaybe d = M.fromMaybe d :<->: \a -> if a == d then Nothing else Just a