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