Maybe Module
Functions for working with Maybe values — composable optional handling.
Maybe represents a value that may or may not exist (Just value or Nothing). The Maybe module provides pipe-friendly functions for transforming, chaining, and extracting Maybe values without explicit pattern matching.
Common patterns
import Maybe
-- Transform the inner value
Just 5 |> Maybe.map (|x| x * 2) -- Just 10
Nothing |> Maybe.map (|x| x * 2) -- Nothing
-- Chain operations that may return Nothing
let safeSqrt = |x| if x >= 0.0 then Just (sqrt x) else Nothing
Just 16.0 |> Maybe.andThen safeSqrt -- Just 4.0
Just (-1.0) |> Maybe.andThen safeSqrt -- Nothing
Nothing |> Maybe.andThen safeSqrt -- Nothing
-- Extract with a default
Just 42 |> Maybe.withDefault 0 -- 42
Nothing |> Maybe.withDefault 0 -- 0
Maybe vs Result
Maybe signals whether a value exists, without explaining why it might be absent. Result carries an error value with the reason for failure.
| Maybe | Result | |
|---|---|---|
| Present | Just value | Ok value |
| Absent | Nothing | Err reason |
| Error info | No | Yes |
Use Maybe for lookups, optional fields, and operations where absence is expected and needs no explanation (List.head, String.toInt). Use Result when the caller needs to know what went wrong. Convert with Result.toMaybe and Result.fromMaybe.
Functions
Inspecting
Maybe.isJust
Maybe a -> Bool
Return true if the Maybe is Just, false if it is Nothing.
import Maybe
Just 5 |> Maybe.isJustTry itNotes: Useful in List.filter: list |> List.filter Maybe.isJust
See also: Maybe.isNothing
Maybe.isNothing
Maybe a -> Bool
Return true if the Maybe is Nothing, false if it is Just.
import Maybe
Nothing |> Maybe.isNothingTry itSee also: Maybe.isJust
Transforming
Maybe.map
(a -> b) -> Maybe a -> Maybe b
Transform the Just value of a Maybe. If the Maybe is Nothing, it is passed through unchanged.
import Maybe
(Just 5) |> Maybe.map (|x| x + 1)Try itSee also: Maybe.andThen, Maybe.withDefault
Chaining
Maybe.andThen
(a -> Maybe b) -> Maybe a -> Maybe b
Chain a Maybe-producing function. If the Maybe is Just, apply the function to the value. If Nothing, pass through.
import Maybe
(Just 5) |> Maybe.andThen (|x| if x > 0 then Just (x * 2) else Nothing)Try itNotes: Useful for chaining operations that may not return a value.
See also: Maybe.map
Filtering
Maybe.filter
(a -> Bool) -> Maybe a -> Maybe a
Keep Just only if the predicate passes; replace with Nothing otherwise. Nothing passes through unchanged.
import Maybe
Just 5 |> Maybe.filter (|x| x > 0)Try itSee also: Maybe.andThen, Result.filter
Combining
Maybe.zip
Maybe a -> Maybe b -> Maybe (a, b)
Combine two Maybes into a Maybe of a tuple. Both must be Just; Nothing if either is absent.
import Maybe
Maybe.zip (Just 1) (Just 2)Try itSee also: Maybe.map, Result.zip
Maybe.flatten
Maybe (Maybe a) -> Maybe a
Remove one layer of Maybe nesting. Just(Just x) becomes Just x; Just(Nothing) becomes Nothing; outer Nothing passes through.
import Maybe
Just (Just 5) |> Maybe.flattenTry itSee also: Maybe.andThen
Maybe.all
[Maybe a] -> Maybe [a]
Collect a list of Maybes into a Maybe of a list. Returns Just with all values if every element is Just; returns Nothing if any element is absent.
import Maybe
[Just 1, Just 2] |> Maybe.allTry itSee also: Maybe.andThen
Extracting
Maybe.withDefault
a -> Maybe a -> a
Extract the Just value, or use the provided default if the Maybe is Nothing.
import Maybe
Nothing
|> Maybe.withDefault 0
-- 0Try itSee also: Maybe.map
Maybe.mapWithDefault
(a -> b) -> b -> Maybe a -> b
Apply a function to the Just value and return the result, or return the default if Nothing. Extracts to a plain value — not a Maybe.
import Maybe
Just 5 |> Maybe.mapWithDefault (|x| x * 2) 0Try itSee also: Maybe.map, Maybe.withDefault