-
Notifications
You must be signed in to change notification settings - Fork 15
/
ex3.hs
21 lines (17 loc) · 646 Bytes
/
ex3.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
instance Foldable Maybe where
-- fold :: Monoid a => Maybe a -> a
fold Nothing = mempty
fold (Just a) = a
-- foldMap :: Monoid b => (a -> b) -> Maybe a -> b
foldMap _ Nothing = mempty
foldMap f (Just a) = f a
-- foldr :: (a -> b -> b) -> b -> Maybe a -> b
foldr _ _ Nothing = mempty
foldr f v (Just a) = f a v
-- foldl :: (a -> b -> a) -> a -> Maybe b -> a
foldl _ _ Nothing = mempty
foldl f v (Just b) = f v b
instance Traversable Maybe where
-- traverse :: Applicative f => (a -> f b) -> Maybe a -> f (Maybe b)
traverse g Nothing = pure Nothing
traverse g (Just a) = Just <$> g a