Result Module
Functions for working with Result values — composable error handling.
Result represents a value that is either successful (Ok value) or an error (Err error). The Result module provides pipe-friendly functions for transforming, chaining, and extracting Result values without explicit pattern matching.
Common patterns
import Result
-- Transform the success value
Ok 5 |> Result.map (|x| x * 2) -- Ok 10
Err "fail" |> Result.map (|x| x * 2) -- Err "fail"
-- Chain operations that can fail
let safeDivide = |x| if x == 0 then Err "div by zero" else Ok (100 / x)
Ok 5 |> Result.andThen safeDivide -- Ok 20
Ok 0 |> Result.andThen safeDivide -- Err "div by zero"
-- Extract with a default
Ok 42 |> Result.withDefault 0 -- 42
Err "oops" |> Result.withDefault 0 -- 0
-- Convert between Result and Maybe
Result.toMaybe (Ok 5) -- Just 5
Result.toMaybe (Err "x") -- Nothing
Result.fromMaybe "missing" (Just 5) -- Ok 5
Result.fromMaybe "missing" Nothing -- Err "missing"
Result vs Maybe
Result carries an error value explaining why something failed. Maybe only signals whether a value exists, with no reason attached.
| Result | Maybe | |
|---|---|---|
| Success | Ok value | Just value |
| Failure | Err reason | Nothing |
| Error info | Yes — the Err holds a value | No — just absent |
Use Result when the caller needs to know what went wrong (parsing, validation, I/O). Use Maybe when absence is routine and needs no explanation (lookups, optional fields). Convert between them with Result.toMaybe (discards the error) and Result.fromMaybe (supplies one).
Functions
Inspecting
Result.isOk
Result a e -> Bool
Return true if the Result is Ok, false if it is Err.
import Result
Ok 5 |> Result.isOkTry itNotes: Useful in List.filter: list |> List.filter Result.isOk
See also: Result.isErr
Result.isErr
Result a e -> Bool
Return true if the Result is Err, false if it is Ok.
import Result
Err "x" |> Result.isErrTry itSee also: Result.isOk
Transforming
Result.map
(a -> b) -> Result a e -> Result b e
Transform the Ok value of a Result. If the Result is Err, it is passed through unchanged.
import Result
(Ok 5) |> Result.map (|x| x + 1)Try itSee also: Result.mapError, Result.andThen
Result.mapError
(e -> f) -> Result a e -> Result a f
Transform the Err value of a Result. If the Result is Ok, it is passed through unchanged.
import Result
(Err "not found") |> Result.mapError (|e| "Error: " ++ e)Try itSee also: Result.map
Chaining
Result.andThen
(a -> Result b e) -> Result a e -> Result b e
Chain a Result-producing function. If the Result is Ok, apply the function to the value. If Err, pass through.
import Result
(Ok 5) |> Result.andThen (|x| if x > 0 then Ok (x * 2) else Err "negative")Try itNotes: Useful for chaining operations that may fail.
See also: Result.map
Filtering
Result.filter
(a -> Bool) -> e -> Result a e -> Result a e
Keep Ok only if the predicate passes; replace with the given Err value otherwise. Err passes through unchanged.
import Result
Ok 5
|> Result.filter (|x| x > 0) "must be positive"
-- Ok 5
Ok -1
|> Result.filter (|x| x > 0) "must be positive"
-- Err "must be positive"Try itSee also: Result.andThen, Maybe.filter
Combining
Result.zip
Result a e -> Result b e -> Result (a, b) e
Combine two Results into a Result of a tuple. Both must be Ok; the first Err wins.
import Result
Result.zip (Ok 1) (Ok 2)Try itSee also: Result.map, Maybe.zip
Result.flatten
Result (Result a e) e -> Result a e
Remove one layer of Result nesting. Ok(Ok x) becomes Ok x; Ok(Err e) becomes Err e; outer Err passes through.
import Result
Ok (Ok 5) |> Result.flattenTry itSee also: Result.andThen
Result.all
[Result a e] -> Result [a] e
Collect a list of Results into a Result of a list. Returns Ok with all values if every element is Ok; returns the first Err encountered otherwise.
import Result
[Ok 1, Ok 2, Ok 3] |> Result.allTry itNotes: For mapping and collecting in one step, use List.map then Result.all.
See also: Result.andThen
Extracting
Result.withDefault
a -> Result a e -> a
Extract the Ok value, or use the provided default if the Result is Err.
import Result
(Err "oops") |> Result.withDefault 0Try itSee also: Result.map
Result.mapWithDefault
(a -> b) -> b -> Result a e -> b
Apply a function to the Ok value and return the result, or return the default if Err. Extracts to a plain value — not a Result.
import Result
Ok 5 |> Result.mapWithDefault (|x| x * 2) 0Try itSee also: Result.map, Result.withDefault
Converting
Result.toMaybe
Result a e -> Maybe a
Convert a Result to a Maybe. Ok becomes Just, Err becomes Nothing.
import Result
Result.toMaybe Ok 5
-- Just 5Try itSee also: Result.fromMaybe
Result.fromMaybe
e -> Maybe a -> Result a e
Convert a Maybe to a Result. Just becomes Ok, Nothing becomes Err with the provided error value.
import Result
Result.fromMaybe "missing" Just 5
-- Ok 5Try itSee also: Result.toMaybe