Esc
Start typing to search...

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.

ResultMaybe
SuccessOk valueJust value
FailureErr reasonNothing
Error infoYes — the Err holds a valueNo — 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.

Example:
import Result

Ok 5 |> Result.isOk
Try it

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

Example:
import Result

Err "x" |> Result.isErr
Try it

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

Example:
import Result

(Ok 5) |> Result.map (|x| x + 1)
Try it

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

Example:
import Result

(Err "not found") |> Result.mapError (|e| "Error: " ++ e)
Try it

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

Example:
import Result

(Ok 5) |> Result.andThen (|x| if x > 0 then Ok (x * 2) else Err "negative")
Try it

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

Example:
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 it

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

Example:
import Result

Result.zip (Ok 1) (Ok 2)
Try it

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

Example:
import Result

Ok (Ok 5) |> Result.flatten
Try it

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

Example:
import Result

[Ok 1, Ok 2, Ok 3] |> Result.all
Try it

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

Example:
import Result

(Err "oops") |> Result.withDefault 0
Try it

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

Example:
import Result

Ok 5 |> Result.mapWithDefault (|x| x * 2) 0
Try it

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

Example:
import Result

Result.toMaybe Ok 5

-- Just 5
Try it

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

Example:
import Result

Result.fromMaybe "missing" Just 5

-- Ok 5
Try it

See also: Result.toMaybe