Prime -> 2010
[haskell-report.git] / report / lib-code / Maybe.hs
1 module Maybe(
2 isJust, isNothing,
3 fromJust, fromMaybe, listToMaybe, maybeToList,
4 catMaybes, mapMaybe,
5
6 -- ...and what the Prelude exports
7 Maybe(Nothing, Just),
8 maybe
9 ) where
10
11 isJust :: Maybe a -> Bool
12 isJust (Just a) = True
13 isJust Nothing = False
14
15 isNothing :: Maybe a -> Bool
16 isNothing = not . isJust
17
18 fromJust :: Maybe a -> a
19 fromJust (Just a) = a
20 fromJust Nothing = error "Maybe.fromJust: Nothing"
21
22 fromMaybe :: a -> Maybe a -> a
23 fromMaybe d Nothing = d
24 fromMaybe d (Just a) = a
25
26 maybeToList :: Maybe a -> [a]
27 maybeToList Nothing = []
28 maybeToList (Just a) = [a]
29
30 listToMaybe :: [a] -> Maybe a
31 listToMaybe [] = Nothing
32 listToMaybe (a:_) = Just a
33
34 catMaybes :: [Maybe a] -> [a]
35 catMaybes ms = [ m | Just m <- ms ]
36
37 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
38 mapMaybe f = catMaybes . map f