@@ -1,4 +1,4 @@
1−module Maybe (Maybe(..), maybe, isJust, isNothing) where
1+module Maybe (Maybe(..), maybe, isJust, isNothing, map) where
22
33{-| Represents an optional value. Maybe it is there, maybe it is not.
44
@@ -8,6 +8,8 @@ module Maybe (Maybe(..), maybe, isJust, isNothing) where
88# Taking Maybes apart
99@docs maybe, isJust, isNothing
1010
11+# Map
12+@docs map
1113-}
1214
1315import Basics (not, (.))
@@ -24,9 +26,6 @@ function to the associated value.
2426
2527 isPositive : Maybe Int -> Bool
2628 isPositive maybeInt = maybe False (\n -> n > 0) maybeInt
27−
28− map : (a -> b) -> Maybe a -> Maybe b
29− map f m = maybe Nothing (\x -> Just (f x)) m
3029-}
3130maybe : b -> (a -> b) -> Maybe a -> b
3231maybe b f m =
@@ -51,3 +50,14 @@ isJust = maybe False (\_ -> True)
5150-}
5251isNothing : Maybe a -> Bool
5352isNothing = not . isJust
53+
54+{-| Transform the contents of a `Maybe` with a given function:
55+
56+ map sqrt (Just 9) == Just 3
57+ map sqrt Nothing == Nothing
58+-}
59+map : (a -> b) -> Maybe a -> Maybe b
60+map f maybe =
61+ case maybe of
62+ Just v -> Just (f v)
63+ Nothing -> Nothing