Standalone Result(T, E) helpers.

Parallel to the Result half of Std.Core, kept separate so that callers can import it without dragging in every combinator. Result(T, E) lowers to {:ok, v} / {:error, e}.

Examples

use Std.Result

ok(42)
  |> map(fn(x) -> x * 2)
  |> and_then(fn(x) -> if x > 0 then ok(x) else error(:neg))
  |> unwrap(0)                          # => 84
use Std.Result

# Recover from errors and collapse nested Results.
or_else(error(:oops), fn(_) -> ok(0))   # => Ok(0)
flatten(ok(ok(7)))                      # => Ok(7)
flatten(ok(error(:inner)))              # => Error(:inner)

Functions

  • # fn __group__() -> Atom

    Group tag consumed by Cure.Stdlib.Preload.

  • # fn and_then(result: Result(T, E), f: (T) -> Result(U, E)) -> Result(U, E)

    Monadic bind on Ok; short-circuits on Error.

  • # fn error(reason: E) -> Result(T, E)

    Build an Error(reason) Result.

  • # fn flatten(result: Result(Result(T, E), E)) -> Result(T, E)

    Collapse a nested Result(Result(T, E), E) into Result(T, E). Useful when and_then produced another Result and you want the inner arm directly.

  • # fn is_error(result: Result(T, E)) -> Bool

    true when the result is in the Error arm.

  • # fn is_ok(result: Result(T, E)) -> Bool

    true when the result is in the Ok arm.

  • # fn map(result: Result(T, E), f: (T) -> U) -> Result(U, E)

    Functorial map over the Ok arm; passes Error through.

  • # fn map_error(result: Result(T, E), f: (E) -> F) -> Result(T, F)

    Functorial map over the Error arm; passes Ok through.

  • # fn ok(value: T) -> Result(T, E)

    Build an Ok(value) Result.

  • # fn or_else(result: Result(T, E), f: (E) -> Result(T, F)) -> Result(T, F)

    Recovery: continue with f(reason) on Error; pass Ok through.

  • # fn unwrap(result: Result(T, E), default: T) -> T

    Extract the Ok value or fall back to default on Error.

  • # fn unwrap_error(result: Result(T, E), default: E) -> E

    Extract the Error reason or fall back to default on Ok.