2 the, exists, theExists, maybe, fromMaybe, listToMaybe, maybeToList,
3 findMaybe, catMaybes, mapMaybe, joinMaybe, unfoldr ) where
5 exists :: Maybe a -> Bool
6 exists = maybe False (const True)
9 the = maybe (error "Maybe.the: Nothing") id
11 theExists :: Maybe a -> (a, Bool)
12 theExists Nothing = (error "Maybe.theExists: Nothing", False)
13 theExists (Just x) = (x, True)
15 fromMaybe :: a -> Maybe a -> a
16 fromMaybe d = maybe d id
18 maybeToList :: Maybe a -> [a]
19 maybeToList = maybe [] (\ x -> [x])
21 listToMaybe :: [a] -> Maybe a
22 listToMaybe [] = Nothing
23 listToMaybe (a:as) = Just a
25 findMaybe :: (a -> Bool) -> [a] -> Maybe a
26 findMaybe p = listToMaybe . filter p
28 catMaybes :: [Maybe a] -> [a]
30 catMaybes (Nothing:xs) = catMaybes xs
31 catMaybes (Just x:xs) = x : catMaybes xs
33 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
34 mapMaybe f = catMaybes . map f
36 joinMaybe :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
37 joinMaybe _ Nothing Nothing = Nothing
38 joinMaybe _ (Just g) Nothing = Just g
39 joinMaybe _ Nothing (Just g) = Just g
40 joinMaybe f (Just g) (Just h) = Just (f g h)
42 -- unfoldr f' (foldr f z xs) == (xs,z)
44 -- if the following holds:
46 -- f' (f x y) = Just (x,y)
48 unfoldr :: (a -> Maybe (b, a)) -> a -> ([b],a)
51 Just (y,x') -> let (ys,x'') = unfoldr f x' in (y:ys,x'')