module Data.Maybe
(
isJust
,isNothing
,fromJust
,fromMaybe
,maybeToList
,listToMaybe
,catMaybes
,mapMaybe
,mapMaybeFB
,whenJust
,whenJust'
,onJust
,joinMaybe)
where
import Prelude
isJust :: Maybe a -> Bool
isJust Nothing = False
isJust _ = True
isNothing :: Maybe a -> Bool
isNothing Nothing = True
isNothing _ = False
fromJust :: Maybe a -> a
fromJust Nothing = error "Maybe.fromJust: Nothing"
fromJust (Just x) = x
fromMaybe :: a -> Maybe a -> a
fromMaybe d x = case x of {Nothing -> d;Just v -> v}
maybeToList :: Maybe a -> [a]
maybeToList Nothing = []
maybeToList (Just x) = [x]
listToMaybe :: [a] -> Maybe a
listToMaybe [] = Nothing
listToMaybe (a:_) = Just a
catMaybes :: [Maybe a] -> [a]
catMaybes ls = [x | Just x <- ls]
mapMaybe :: (a -> Maybe b) -> [a] -> [b]
mapMaybe _ [] = []
mapMaybe f (x:xs) =
let rs = mapMaybe f xs in
case f x of
Nothing -> rs
Just r -> r:rs
mapMaybeFB :: (b -> r -> r) -> (a -> Maybe b) -> a -> r -> r
mapMaybeFB cons f x next = case f x of
Nothing -> next
Just r -> cons r next
whenJust :: Maybe a -> (a -> Fay ()) -> Fay ()
whenJust (Just x) f = f x
whenJust Nothing _ = return ()
whenJust' :: Maybe a -> (a -> Fay b) -> Fay (Maybe b)
whenJust' (Just x) f = f x >>= return . Just
whenJust' Nothing _ = return Nothing
onJust :: (a -> b) -> Maybe a -> Maybe b
onJust f (Just x) = Just (f x)
onJust _ Nothing = Nothing
joinMaybe :: Maybe (Maybe a) -> Maybe a
joinMaybe (Just (Just x)) = Just x
joinMaybe _ = Nothing