elm-community/maybe-extra/5.1.0

NOTE: the latest version is 5.3.0

Maybe.Extra

Convenience functions for Maybe.

Basics

Work with 1 Maybe

isJust : Maybe a -> Bool
isJust (Just 42)
--> True


isJust (Just [])
--> True


isJust Nothing
--> False
isNothing : Maybe a -> Bool
isNothing (Just 42)
--> False


isNothing (Just [])
--> False


isNothing Nothing
--> True
join : Maybe (Maybe a) -> Maybe a

Flattens nested Maybes

join (Just (Just 1))
--> Just 1


join (Just Nothing)
--> Nothing


join Nothing
--> Nothing
filter : (a -> Bool) -> Maybe a -> Maybe a

Keep the Maybe only if the predicate function passes

filter (\v -> v == 1) (Just 1)
--> Just 1


filter (\v -> v == 2) (Just 1)
--> Nothing


filter (\v -> v == 1) Nothing
--> Nothing

Get a value out of a Maybe

unwrap : b -> (a -> b) -> Maybe a -> b

Apply the function to the value in the Maybe and return it unwrapped. If the Maybe is Nothing, use the default value instead.

unwrap default f is equivalent to Maybe.map f >> Maybe.withDefault default.

unwrap 0 String.length Nothing
--> 0


unwrap 0 String.length (Just "abc")
--> 3
unpack : (() -> b) -> (a -> b) -> Maybe a -> b

Like unwrap, but the default value is lazy, and will only be computed if the Maybe is Nothing.

unpack (\() -> 0) String.length Nothing
--> 0


unpack (\() -> 0) String.length (Just "abc")
--> 3

Or

Take the first value that's present

or : Maybe a -> Maybe a -> Maybe a

Returns the first value that is present, like the boolean ||.

Both values will be computed. There is no short-circuiting. If your second argument is expensive to calculate and you need short circuiting, use orLazy instead.

or (Just 4) (Just 5)
--> Just 4


or (Just 4) Nothing
--> Just 4


or Nothing (Just 5)
--> Just 5


or Nothing Nothing
--> Nothing

Advanced functional programmers will recognize this as the implementation of mplus for Maybes from the MonadPlus type class.

orElse : Maybe a -> Maybe a -> Maybe a

Piping-friendly version of or.

Just 5
    |> orElse (Just 4)
--> Just 5


orElse (Just 4) (Just 5)
--> Just 5


List.head []
    |> orElse (List.head [ 4 ])
--> Just 4
orList : List (Maybe a) -> Maybe a

Returns the first value that is present.

All values will be computed. If your arguments are expensive to calculate, use orListLazy instead.

orList
    [ Nothing
    , Just 1
    , Just 2
    ]
--> Just 1


orList
    [ List.head []
    , String.toInt ""
    ]
--> Nothing


orList []
--> Nothing
orLazy : Maybe a -> (() -> Maybe a) -> Maybe a

Lazy version of or.

The second argument will only be evaluated if the first argument is Nothing.

orLazy (Just 4) (\() -> Debug.todo "Expensive calculation")
--> Just 4
orElseLazy : (() -> Maybe a) -> Maybe a -> Maybe a

Lazy version of orElse. Piping-friendly version of orLazy.

The first argument will only be evaluated if the second argument is Nothing.

Just 4
    |> orElseLazy (\() -> Debug.todo "Expensive calculation")
--> Just 4
orListLazy : List (() -> Maybe a) -> Maybe a

Lazy version of orList

Stops calculating new values after the first match

orListLazy
    [ \() -> Nothing
    , \() -> Just 1
    , \() -> Debug.todo "Expensive calculation"
    ]
--> Just 1
oneOf : List (a -> Maybe b) -> a -> Maybe b

Try a list of functions against a value. Return the value of the first call that succeeds (returns Just).

type UserInput
    = FloatInput Float
    | IntInput Int
    | UnknownInput


"5.6"
    |> oneOf
        [ String.toInt >> Maybe.map IntInput
        , String.toFloat >> Maybe.map FloatInput
        ]
    |> Maybe.withDefault UnknownInput
--> FloatInput 5.6

Lists of Maybes

values : List (Maybe a) -> List a

Take all the values that are present, throwing away any Nothings.

Equivalent to List.filterMap identity.

values [ Just 1, Nothing, Just 2 ]
--> [ 1, 2 ]
combine : List (Maybe a) -> Maybe (List a)

If every Maybe in the list is present, return all of the values unwrapped. If there are any Nothings, the whole function fails and returns Nothing.

combine []
--> Just []


combine [ Just 1, Just 2, Just 3 ]
--> Just [ 1, 2, 3 ]


combine [ Just 1, Nothing, Just 3 ]
--> Nothing
traverse : (a -> Maybe b) -> List a -> Maybe (List b)

Like combine, but map a function over each element of the list first.

If every function call succeeds (returns Just), traverse will return a list. If any function call fails (returns Nothing), traverse will return Nothing.

combine is equivalent to traverse identity.

traverse (\x -> Just (x * 10)) [ 1, 2, 3, 4, 5 ]
--> Just [ 10, 20, 30, 40, 50 ]


traverse List.head [ [1], [2, 3], [] ]
--> Nothing
combineArray : Array (Maybe a) -> Maybe (Array a)

Like combine, but works on Array instead of List.

traverseArray : (a -> Maybe b) -> Array a -> Maybe (Array b)

Like traverse, but works on Array instead of List.

toList

toList : Maybe a -> List a

A Maybe is a lot like a list that can only be length 0 or 1.

Returns a singleton list if the value is present, and an empty list it's missing.

toList Nothing
--> []


toList (Just 1)
--> [ 1 ]
toArray : Maybe a -> Array a

Like toList, but returns a singleton or empty Array.

import Array


toArray Nothing
--> Array.fromList []


toArray (Just 1)
--> Array.fromList [ 1 ]
cons : Maybe a -> List a -> List a

Add an item to a list only if it's a Just.

cons (Just 1) [ 2, 3 ]
--> [ 1, 2, 3 ]


cons Nothing [2, 3 ]
--> [ 2, 3 ]

Applicative Functions

andMap : Maybe a -> Maybe (a -> b) -> Maybe b

If both a function and a value are present, apply the function to the value. If either argument is Nothing, return Nothing.

Just ((+) 2)
    |> andMap (Just 3)
--> Just 5


Nothing
    |> andMap (Just 3)
--> Nothing


Just ((+) 2)
    |> andMap Nothing
--> Nothing

Advanced functional programmers will recognize this as the implementation of <*> for Maybes from the Applicative typeclass.