Convenience functions for Maybe.
Flattens nested Maybes
join (Just (Just 1)) == Just 1
join (Just Nothing) == Nothing
join Nothing == NothingConveniently check if a Maybe matches Nothing.
isNothing (Just 42) == False
isNothing (Just []) == False
isNothing Nothing == TrueConveniently check if a Maybe matches Just _.
isJust (Just 42) == True
isJust (Just []) == True
isJust Nothing == FalseTake a default value, a function and a Maybe.
Return the default value if the Maybe is Nothing.
If the Maybe is Just a, apply the function on a and return the b.
That is, unwrap d f is equivalent to Maybe.map f >> Maybe.withDefault d.
A version of unwrap that is non-strict in the default value (by
having it passed in a thunk).
Take a Maybe and a predicate function and return a Maybe with the original value when a predicate matches.
filter (\v -> v == 1) (Just 1) == Just 1
filter (\v -> v == 2) (Just 1) == NothingApply the function that is inside Maybe to a value that is inside Maybe. Return the result inside Maybe. If one of the Maybe arguments is Nothing, return Nothing.
Just ((+) 2) |> andMap (Just 3) == Just 5
Nothing |> andMap (Just 3) == Nothing
Just ((+) 2) |> andMap Nothing == NothingAdvanced functional programmers will recognize this as the implementation of <*> for Maybes from the Applicative typeclass.
Take two Maybe values. If the first one equals Nothing, return Nothing. Otherwise return the second value.
next (Just 1) (Just 2) == Just 2
next Nothing (Just 2) == Nothing
next (Just 1) Nothing == NothingAdvanced functional programmers will recognize this as the implementation of *> for Maybes from the Applicative typeclass.
Take two Maybe values. If the second one equals Nothing, return Nothing. Otherwise return the first value.
prev (Just 1) (Just 2) == Just 1
prev Nothing (Just 2) == Nothing
prev (Just 1) Nothing == NothingAdvanced functional programmers will recognize this as the implementation of <* for Maybes from the Applicative typeclass.
Like the boolean || this will return the first value that is
positive (Just). However, unlike with ||, both values will be
computed anyway (there is no short-circuiting).
Just 4 `or` Just 5 == Just 4
Just 4 `or` Nothing == Just 4
Nothing `or` Just 5 == Just 5
Nothing `or` Nothing == NothingThis function sort of works like oneOf but on single Maybes.
Advanced functional programmers will recognize this as the
implementation of mplus for Maybes from the MonadPlus type
class.
Non-strict version of or. The second argument will only be
evaluated if the first argument is Nothing.
Piping-friendly version of orLazy. The first argument will only
be evaluated if the second argument is Nothing. Example use:
List.head []
|> orElseLazy (\() -> List.head [4])Strict version of orElseLazy (and at the same time,
piping-friendly version of or).
orElse (Just 4) (Just 5) == Just 5 -- crucial difference from `or`
orElse (Just 4) Nothing == Just 4
orElse Nothing (Just 5) == Just 5
orElse Nothing Nothing == NothingAlso:
List.head []
|> orElse (List.head [4])Return an empty list on Nothing or a list with one element, where the element is the value of Just.
maybeToList Nothing == []
maybeToList (Just 1) == [1]Return an empty array on Nothing or a list with one element, where the element is the value of Just.
maybeToArray Nothing == Array.fromList []
maybeToArray (Just 1) == Array.fromList [1]Take a function that returns Maybe value and a list. Map a function over each element of the list. Collect the result in the list within Maybe.
traverse (\x -> Just (x*10)) [1,2,3,4,5] == Just [10,20,30,40,50]Take a list of Maybes and return a Maybe with a list of values. combine == traverse identity.
combine [] == Just []
combine [Just 1, Just 2, Just 3] == Just [1,2,3]
combine [Just 1, Nothing, Just 3] == Nothing
Flipped, infix version of
withDefault.