krisajenkins/elm-exts/27.4.0

NOTE: this package is not compatible with Elm 0.19.1

Exts.Maybe

Extensions to the core Maybe library.

isJust : Maybe a -> Bool

Boolean checks.

isNothing : Maybe a -> 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 Maybes 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 -> 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 -> Bool) -> a -> Maybe a

Validate a value against a predicate, returning a Maybe.

validate isEven 2 => Just 2
validate isEven 3 => Nothing
when : 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.