isNothing (Just 42)
--> False
isNothing (Just [])
--> False
isNothing Nothing
--> TrueFlattens nested Maybes
join (Just (Just 1))
--> Just 1
join (Just Nothing)
--> Nothing
join Nothing
--> NothingKeep 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
--> NothingMaybeLazy version of Maybe.withDefault.
It will only calculate the default if needed.
Examples:
withDefaultLazy (\() -> 2 + 2) Nothing
--> 4
withDefaultLazy (\() -> Debug.todo "Expensive calculation") (Just 4)
--> 4Like using a case.
Give a function that says what to do if the input is Just,
and a value to use if the input is Nothing.
These are all equivalent:
unwrap default f maybeX
maybeX
|> Maybe.map f
|> Maybe.withDefault default
case maybeX of
Just x ->
f x
Nothing ->
defaultExcept that unlike a case, the default value for unwrap is always computed.
If your default value is expensive to compute, use the lazy unpack instead.
Examples:
unwrap 0 String.length Nothing
--> 0
unwrap 0 String.length (Just "abc")
--> 3Like 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")
--> 3unpack (\() -> default) f maybeX is equivalent to
case maybeX of
Just x ->
f x
Nothing ->
defaultTake the first value that's present
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
--> NothingAdvanced functional programmers will recognize this as the
implementation of mplus for Maybes from the MonadPlus type
class.
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 4Returns 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 []
--> NothingLazy version of or.
The second argument will only be evaluated if the first argument is Nothing.
orLazy (Just 4) (\() -> Debug.todo "Expensive calculation")
--> Just 4Lazy version of orList
Stops calculating new values after the first match
orListLazy
[ \() -> Nothing
, \() -> Just 1
, \() -> Debug.todo "Expensive calculation"
]
--> Just 1Try 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.6MaybesTake all the values that are present, throwing away any Nothings.
Equivalent to List.filterMap identity.
values [ Just 1, Nothing, Just 2 ]
--> [ 1, 2 ]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 ]
--> NothingLike combine, but map a function over each element of the list first.
If every function call succeeds (returns Just), combineMap will return a list.
If any function call fails (returns Nothing), combineMap will return Nothing.
combine is equivalent to combineMap identity.
combineMap (\x -> Just (x * 10)) [ 1, 2, 3, 4, 5 ]
--> Just [ 10, 20, 30, 40, 50 ]
combineMap List.head [ [1], [2, 3], [] ]
--> NothingLike combineMap,
but works on Array instead of List.
Pull a maybe out of the first element of a tuple and combine it into a maybe holding the tuple's values.
Pull a result out of the second element of a tuple
and combine it into a result holding the tuple's values.
Also known as sequence on tuples.
Combine all maybes in a tuple into a single maybe holding the tuple's values.
Map a function producing maybes on the first element of a tuple
and then pull it out using combineFirst.
combineMapFirst f ( x, y )
== combineFirst (Tuple.mapFirst f ( x, y ))
== Maybe.map (\newX -> ( newX, y )) (f x)Map a function producing maybes on the second element of a tuple
and then pull it out using combineSecond.
combineMapSecond f ( x, y )
== combineSecond (Tuple.mapSecond f ( x, y ))
== Maybe.map (Tuple.pair x) (f y)Map a function producing maybes on the both elements of a tuple
and then pull them out using combineBoth.
combineMapBoth f g ( x, y )
== combineBoth (Tuple.mapBoth f g ( x, y ))
== Maybe.map2 Tuple.pair (f x) (g y)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 ]Like toList, but returns a singleton or empty Array.
import Array
toArray Nothing
--> Array.fromList []
toArray (Just 1)
--> Array.fromList [ 1 ]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 ]import Array exposing (Array)
array : Array Int
array = Array.fromList [1,2,3]
andThen2 Array.get (Just 1) (Just array)
--> Just 2
andThen2 Array.get Nothing (Just array)
--> Nothing
andThen2 Array.get (Just 1) Nothing
--> Nothing
andThen2 Array.get (Just 4) (Just array)
--> NothingandThen for 3 maybes.
andThen for 4 maybes.
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
--> NothingThis can be used to do Maybe.mapN or andThenN for any number of arguments.
-- map4
Just (\a b c d -> a + b + c + d )
|> andMap (Just 1)
|> andMap (Just 2)
|> andMap (Just 4)
|> andMap (Just 8)
--> Just 15
-- andThen4
Just (\a b c d -> Just (a + b + c + d ))
|> andMap (Just 1)
|> andMap (Just 2)
|> andMap (Just 4)
|> andMap (Just 8)
|> join
--> Just 15Advanced 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.