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, mapDefault d f is equivalent to Maybe.map f >> Maybe.withDefault d.
Apply 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
Just 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').
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 'Maybe's.
Advanced functional programmers will recognize this as the implementation of 'mplus' for Maybes from the 'MonadPlus' Typeclass.
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.