@@ -1,4 +1,4 @@
1−module Maybe (Maybe(..), maybe, isJust, isNothing) where
1+module Maybe (Maybe(..), maybe, isJust, isNothing, map) where
3{-| Represents an optional value. Maybe it is there, maybe it is not.
3{-| Represents an optional value. Maybe it is there, maybe it is not.
@@ -8,6 +8,8 @@ module Maybe (Maybe(..), maybe, isJust, isNothing) where
8# Taking Maybes apart
8# Taking Maybes apart
9@docs maybe, isJust, isNothing
9@docs maybe, isJust, isNothing
13import Basics (not, (.))
15import Basics (not, (.))
@@ -24,9 +26,6 @@ function to the associated value.
25 isPositive : Maybe Int -> Bool
27 isPositive : Maybe Int -> Bool
26 isPositive maybeInt = maybe False (\n -> n > 0) maybeInt
28 isPositive maybeInt = maybe False (\n -> n > 0) maybeInt
28− map : (a -> b) -> Maybe a -> Maybe b
29− map f m = maybe Nothing (\x -> Just (f x)) m
31maybe : b -> (a -> b) -> Maybe a -> b
30maybe : b -> (a -> b) -> Maybe a -> b
32maybe b f m =
31maybe b f m =
@@ -51,3 +50,14 @@ isJust = maybe False (\_ -> True)
52isNothing : Maybe a -> Bool
51isNothing : Maybe a -> Bool
53isNothing = not . isJust
52isNothing = not . isJust
54+{-| Transform the contents of a `Maybe` with a given function:
56+ map sqrt (Just 9) == Just 3
57+ map sqrt Nothing == Nothing
59+map : (a -> b) -> Maybe a -> Maybe b