Extensions to the core Maybe
library.
isJust : Maybe a -> Basics.Bool
Boolean checks.
isNothing : Maybe a -> Basics.Bool
maybe : b -> (a -> b) -> Maybe a -> b
Apply a function to a value, returning the default if the value is Nothing
.
Example:
greeting : Maybe User -> String
greeting maybeUser =
case maybeUser of
Just user ->
user.name
Nothing ->
"Guest"
...could be replaced with:
greeting : Maybe User -> String
greeting user =
maybe "Guest" .name user
...or even:
greeting : Maybe User -> String
greeting =
maybe "Guest" .name
Aside: There's always a judgement call to be made here. Is shorter code clearer (because it removes common plumbing, leaving only meaning), or is it harder to understand (because people can't see how the plumbing works anymore)? Learn both ways, choose with your eyes open, and stay tasteful out there.
mappend : Maybe a -> Maybe b -> Maybe ( a, b )
Join two Maybe
s together as though they were one.
catMaybes : List (Maybe a) -> List a
Extract all the Just
values from a List of Maybes.
join : (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
Join together two Maybe
values using the supplied function. If
either value is Nothing
, the result is Nothing
.
maybeDefault : a -> Maybe a -> Maybe a
If x
is a Just _
value, return it, otherwise return Just default
.
matches : (a -> Basics.Bool) -> Maybe a -> Maybe a
Check the if value in the Maybe
matches a predicate. If it does, pass it through, if not, return nothing.
matches isEven (Just 2) => Just 2
matches isEven (Just 3) => Nothing
matches isEven Nothing => Nothing
validate : (a -> Basics.Bool) -> a -> Maybe a
Validate a value against a predicate, returning a Maybe
.
validate isEven 2 => Just 2
validate isEven 3 => Nothing
when : Basics.Bool -> a -> Maybe a
When test
returns true, return Just value
, otherwise return Nothing
.
oneOf : List (Maybe a) -> Maybe a
Return the first non-Nothing
entry in the list.