on master help sign in
Add more examples

Based on very valid observation here (https://groups.google.com/forum/#!topic/elm-discuss/_vvE0OWNKDU) that these docs were bad before.

commit57c4b79eb34e9b9a6dde9ca621a996ec0a458ae3 authorEvan Czaplicki <info@elm-lang.org> dateTue 18 Feb 2014, 18:06 (13 years ago) parent8aba726
1 file changed+21−3
libraries/Maybe.elm+21−3
libraries/Maybe.elm+21−3
@@ -21,27 +21,45 @@ numbers).
2121-}
2222data Maybe a = Just a | Nothing
2323
24−{-| Apply a function to the contents of a `Maybe`.
25−Return default when given `Nothing`.
24+{-| Provide a default value and a function to extract the contents of a `Maybe`.
25+When given `Nothing` you get the default, when given a `Just` you apply the
26+function to the associated value.
27+
28+ isPositive : Maybe Int -> Bool
29+ isPositive maybeInt = maybe False (\n -> n > 0) maybeInt
30+
31+ map : (a -> b) -> Maybe a -> Maybe b
32+ map f m = maybe Nothing (\x -> Just (f x)) m
2633-}
2734maybe : b -> (a -> b) -> Maybe a -> b
2835maybe b f m = case m of
2936 Just v -> f v
3037 Nothing -> b
3138
32−{-| Check if constructed with `Just`.
39+{-| Check if a maybe happens to be a `Just`.
40+
41+ isJust (Just 42) == True
42+ isJust (Just []) == True
43+ isJust Nothing == False
3344-}
3445isJust : Maybe a -> Bool
3546isJust = maybe False (\_ -> True)
3647
3748{-| Check if constructed with `Nothing`.
49+
50+ isNothing (Just 42) == False
51+ isNothing (Just []) == False
52+ isNothing Nothing == True
3853-}
3954isNothing : Maybe a -> Bool
4055isNothing = not . isJust
4156
57+cons : Maybe a -> [a] -> [a]
4258cons mx xs = maybe xs (\x -> x :: xs) mx
4359
4460{-| Filters out Nothings and extracts the remaining values.
61+
62+ justs [Just 0, Nothing, Just 5, Just 7] == [0,5,7]
4563-}
4664justs : [Maybe a] -> [a]
4765justs = foldr cons []