X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Futils%2FMaybes.lhs;h=39e6185a19eedbc355a31573e951e5ac0f05994b;hp=1f443db28d6b83202044e3543e9497c5f5f7cca2;hb=8133a9f47b99f4e65ed30551de32ad72c6b61b27;hpb=a4cb65161fd2c34e4455985490220bdf5d30cbe5 diff --git a/compiler/utils/Maybes.lhs b/compiler/utils/Maybes.lhs index 1f443db..39e6185 100644 --- a/compiler/utils/Maybes.lhs +++ b/compiler/utils/Maybes.lhs @@ -14,7 +14,7 @@ module Maybes ( orElse, mapCatMaybes, allMaybes, - firstJust, + firstJust, firstJusts, expectJust, maybeToBool, @@ -46,12 +46,14 @@ allMaybes (Just x : ms) = case allMaybes ms of 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. -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} @@ -70,6 +72,7 @@ mapCatMaybes f (x:xs) = case f x of \end{code} \begin{code} + orElse :: Maybe a -> a -> a (Just x) `orElse` _ = x Nothing `orElse` y = y