An extended toolbox for working with maybes: everything from the elm/core
Maybe module plus Haskell-inspired helpers such as maybe, isJust and
maybeToList, all implemented the Elm way.
Provide a default value, turning an optional value into a normal
value.
Alias of withDefault.
Take a default value, a function, and a Maybe value. If the Maybe value
is Nothing, the function returns the default value. Otherwise, it applies
the function to the value inside the Just and returns the result.
Basic usage:
maybe False odd (Just 3) == True
maybe False odd Nothing == FalseRead an integer from a string. If we succeed, return twice the integer; that is, apply ((*) 2) to it. If instead we fail to parse an integer, return 0 by default:
maybe 0 ((*) 2) (String.toInt "5") == 10
maybe 0 ((*) 2) (String.toInt "") == 0Apply fromInt to a Maybe Int. If we have Just n,
we want to show the underlying Int n. But if we have Nothing, we return
the empty string instead of (for example) "Nothing":
maybe "" String.fromInt (Just 5) == "5"
maybe "" String.fromInt Nothing == ""Return True iff its argument is of the form Just _.
Basic usage:
isJust (Just 3) == True
isJust (Just ()) == True
isJust Nothing == FalseOnly the outer constructor is taken into consideration:
isJust (Just Nothing) == TrueReturn True iff its argument is Nothing.
Basic usage:
isNothing (Just 3) == False
isNothing (Just ()) == False
isNothing Nothing == TrueOnly the outer constructor is taken into consideration:
isNothing (Just Nothing) == FalseTransform a Maybe value with a given function:
map sqrt (Just 9) == Just 3
map sqrt Nothing == Nothing
map sqrt (String.toFloat "9") == Just 3
map sqrt (String.toFloat "x") == NothingApply a function if all the arguments are Just a value.
map2 (+) (Just 3) (Just 4) == Just 7
map2 (+) (Just 3) Nothing == Nothing
map2 (+) Nothing (Just 4) == Nothing
map2 (+) (String.toInt "1") (String.toInt "123") == Just 124
map2 (+) (String.toInt "x") (String.toInt "123") == Nothing
map2 (+) (String.toInt "1") (String.toInt "1.3") == NothingReturn Nothing on an empty list or Just a where a is the first element
of the list.
Basic usage:
listToMaybe [] == Nothing
listToMaybe [ 9 ] == Just 9
listToMaybe [ 1, 2, 3 ] == Just 1Composing maybeToList with listToMaybe should be the identity on singleton/empty lists:
(maybeToList <| listToMaybe [ 5 ]) == [ 5 ]
(maybeToList <| listToMaybe []) == []But not on lists with more than one element:
(maybeToList <| listToMaybe [ 1, 2, 3 ]) == [ 1 ]Return an empty list when given Nothing or a singleton list when
given Just.
Basic usage:
maybeToList (Just 7) == [ 7 ]
maybeToList Nothing == []One can use maybeToList to avoid pattern matching when combined with a function that (safely) works on lists:
from Data.List import sum
(sum <| maybeToList (String.toInt "3")) == 3
(sum <| maybeToList (String.toInt "")) == 0Take a list of Maybes and return a list of all the Just values.
Basic usage:
catMaybes [ Just 1, Nothing, Just 3 ] == [ 1, 3 ]When constructing a list of Maybe values, catMaybes can be used to return all of the "success" results (if the list is the result of a map, then mapMaybe would be more appropriate):
catMaybes (map String.toInt [ "1", "Foo", "3" ]) == [ 1, 3 ]Apply the given function to each element of the list and collect the
results that are Just _, discarding any Nothings.
Equivalent to to filterMap.
Using mapMaybe f x is a shortcut for catMaybes $ map f x in most cases:
mapMaybe String.toInt [ "1", "Foo", "3" ] == [ 1, 3 ]
catMaybes (map readMaybeInt [ "1", "Foo", "3" ]) == [ 1, 3 ]If we map the Just constructor, the entire list should be returned:
mapMaybe Just [ 1, 2, 3 ] == [ 1, 2, 3 ]Chain together many computations that may fail. It is helpful to see its definition:
andThen : (a -> Maybe b) -> Maybe a -> Maybe b
andThen callback maybe =
case maybe of
Just value ->
callback value
Nothing ->
NothingThis means we only continue with the callback if things are going well. For example, say you need to parse some user input as a month:
parseMonth : String -> Maybe Int
parseMonth userInput =
String.toInt userInput
|> andThen toValidMonth
toValidMonth : Int -> Maybe Int
toValidMonth month =
if 1 <= month && month <= 12 then
Just month
else
NothingIn the parseMonth function, if String.toInt produces Nothing (because
the userInput was not an integer) this entire chain of operations will
short-circuit and result in Nothing. If toValidMonth results in Nothing,
again the chain of computations will result in Nothing.
Provide a default value, turning an optional value into a normal value. This comes in handy when paired with functions like
Dict.getwhich gives back aMaybe.Note: This can be overused! Many cases are better handled by a
caseexpression. And if you end up usingwithDefaulta lot, it can be a good sign that a custom type will clean your code up quite a bit!