Skip to content
Halyard
Project
Source code on Codeberg (opens in new tab)

Search

Menu

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

halyard
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.

MaybeResult
PresentJust valueOk value
AbsentNothingErr reason
Error infoNoYes

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.

Example:
halyard
import Maybe

Just 5 |> Maybe.isJust

Notes: 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.

Example:
halyard
import Maybe

Nothing |> Maybe.isNothing

See also: Maybe.isJust

Transforming

Maybe.map

(a ->{| e} b) -> Maybe a ->{| e} Maybe b

Transform the Just value of a Maybe. If the Maybe is Nothing, it is passed through unchanged.

Example:
halyard
import Maybe

(Just 5) |> Maybe.map (|x| x + 1)

See also: Maybe.andThen, Maybe.withDefault

Chaining

Maybe.andThen

(a ->{| e} Maybe b) -> Maybe a ->{| e} Maybe b

Chain a Maybe-producing function. If the Maybe is Just, apply the function to the value. If Nothing, pass through.

Example:
halyard
import Maybe

(Just 5) |> Maybe.andThen (|x| if x > 0 then Just (x * 2) else Nothing)

Notes: Useful for chaining operations that may not return a value.

See also: Maybe.map

Filtering

Maybe.filter

(a ->{| e} Bool) -> Maybe a ->{| e} Maybe a

Keep Just only if the predicate passes; replace with Nothing otherwise. Nothing passes through unchanged.

Example:
halyard
import Maybe

Just 5 |> Maybe.filter (|x| x > 0)

See 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.

Example:
halyard
import Maybe

Maybe.zip (Just 1) (Just 2)

See 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.

Example:
halyard
import Maybe

Just (Just 5) |> Maybe.flatten

See 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.

Example:
halyard
import Maybe

[Just 1, Just 2] |> Maybe.all

See also: Maybe.andThen

Extracting

Maybe.withDefault

a -> Maybe a -> a

Extract the Just value, or use the provided default if the Maybe is Nothing.

Example:
halyard
import Maybe

Nothing
    |> Maybe.withDefault 0

-- 0

See also: Maybe.map

Maybe.mapWithDefault

(a ->{| e} b) -> b -> Maybe a ->{| e} 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.

Example:
halyard
import Maybe

Just 5 |> Maybe.mapWithDefault (|x| x * 2) 0

See also: Maybe.map, Maybe.withDefault