projects
/
ghc-hetmet.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
White space only
[ghc-hetmet.git]
/
compiler
/
utils
/
Maybes.lhs
diff --git
a/compiler/utils/Maybes.lhs
b/compiler/utils/Maybes.lhs
index
8a6a5f4
..
39e6185
100644
(file)
--- a/
compiler/utils/Maybes.lhs
+++ b/
compiler/utils/Maybes.lhs
@@
-10,10
+10,11
@@
module Maybes (
MaybeErr(..), -- Instance of Monad
failME, isSuccess,
MaybeErr(..), -- Instance of Monad
failME, isSuccess,
+ fmapM_maybe,
orElse,
mapCatMaybes,
allMaybes,
orElse,
mapCatMaybes,
allMaybes,
- firstJust,
+ firstJust, firstJusts,
expectJust,
maybeToBool,
expectJust,
maybeToBool,
@@
-45,12
+46,14
@@
allMaybes (Just x : ms) = case allMaybes ms of
Nothing -> Nothing
Just xs -> Just (x:xs)
Nothing -> Nothing
Just xs -> Just (x:xs)
+firstJust :: Maybe a -> Maybe a -> Maybe a
+firstJust (Just a) _ = Just a
+firstJust Nothing b = b
+
-- | Takes a list of @Maybes@ and returns the first @Just@ if there is one, or
-- @Nothing@ otherwise.
-- | Takes a list of @Maybes@ and returns the first @Just@ if there is one, or
-- @Nothing@ otherwise.
-firstJust :: [Maybe a] -> Maybe a
-firstJust [] = Nothing
-firstJust (Just x : _) = Just x
-firstJust (Nothing : ms) = firstJust ms
+firstJusts :: [Maybe a] -> Maybe a
+firstJusts = foldr firstJust Nothing
\end{code}
\begin{code}
\end{code}
\begin{code}
@@
-69,11
+72,20
@@
mapCatMaybes f (x:xs) = case f x of
\end{code}
\begin{code}
\end{code}
\begin{code}
+
orElse :: Maybe a -> a -> a
(Just x) `orElse` _ = x
Nothing `orElse` y = y
\end{code}
orElse :: Maybe a -> a -> a
(Just x) `orElse` _ = x
Nothing `orElse` y = y
\end{code}
+\begin{code}
+fmapM_maybe :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)
+fmapM_maybe _ Nothing = return Nothing
+fmapM_maybe f (Just x) = do
+ x' <- f x
+ return $ Just x'
+\end{code}
+
%************************************************************************
%* *
\subsection[MaybeT type]{The @MaybeT@ monad transformer}
%************************************************************************
%* *
\subsection[MaybeT type]{The @MaybeT@ monad transformer}