Add more examples
Based on very valid observation here (https://groups.google.com/forum/#!topic/elm-discuss/_vvE0OWNKDU) that these docs were bad before.
This commit is contained in:
parent
8aba726d22
commit
57c4b79eb3
1 changed files with 21 additions and 3 deletions
|
@ -21,27 +21,45 @@ numbers).
|
||||||
-}
|
-}
|
||||||
data Maybe a = Just a | Nothing
|
data Maybe a = Just a | Nothing
|
||||||
|
|
||||||
{-| Apply a function to the contents of a `Maybe`.
|
{-| Provide a default value and a function to extract the contents of a `Maybe`.
|
||||||
Return default when given `Nothing`.
|
When given `Nothing` you get the default, when given a `Just` you apply the
|
||||||
|
function to the associated value.
|
||||||
|
|
||||||
|
isPositive : Maybe Int -> Bool
|
||||||
|
isPositive maybeInt = maybe False (\n -> n > 0) maybeInt
|
||||||
|
|
||||||
|
map : (a -> b) -> Maybe a -> Maybe b
|
||||||
|
map f m = maybe Nothing (\x -> Just (f x)) m
|
||||||
-}
|
-}
|
||||||
maybe : b -> (a -> b) -> Maybe a -> b
|
maybe : b -> (a -> b) -> Maybe a -> b
|
||||||
maybe b f m = case m of
|
maybe b f m = case m of
|
||||||
Just v -> f v
|
Just v -> f v
|
||||||
Nothing -> b
|
Nothing -> b
|
||||||
|
|
||||||
{-| Check if constructed with `Just`.
|
{-| Check if a maybe happens to be a `Just`.
|
||||||
|
|
||||||
|
isJust (Just 42) == True
|
||||||
|
isJust (Just []) == True
|
||||||
|
isJust Nothing == False
|
||||||
-}
|
-}
|
||||||
isJust : Maybe a -> Bool
|
isJust : Maybe a -> Bool
|
||||||
isJust = maybe False (\_ -> True)
|
isJust = maybe False (\_ -> True)
|
||||||
|
|
||||||
{-| Check if constructed with `Nothing`.
|
{-| Check if constructed with `Nothing`.
|
||||||
|
|
||||||
|
isNothing (Just 42) == False
|
||||||
|
isNothing (Just []) == False
|
||||||
|
isNothing Nothing == True
|
||||||
-}
|
-}
|
||||||
isNothing : Maybe a -> Bool
|
isNothing : Maybe a -> Bool
|
||||||
isNothing = not . isJust
|
isNothing = not . isJust
|
||||||
|
|
||||||
|
cons : Maybe a -> [a] -> [a]
|
||||||
cons mx xs = maybe xs (\x -> x :: xs) mx
|
cons mx xs = maybe xs (\x -> x :: xs) mx
|
||||||
|
|
||||||
{-| Filters out Nothings and extracts the remaining values.
|
{-| Filters out Nothings and extracts the remaining values.
|
||||||
|
|
||||||
|
justs [Just 0, Nothing, Just 5, Just 7] == [0,5,7]
|
||||||
-}
|
-}
|
||||||
justs : [Maybe a] -> [a]
|
justs : [Maybe a] -> [a]
|
||||||
justs = foldr cons []
|
justs = foldr cons []
|
||||||
|
|
Loading…
Reference in a new issue