Std. Result
(Cure Standard Library v0.37.2)
View Source
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}.
Extractor naming
The stdlib uses one extractor convention: unwrap for the Option
happy path, unwrap_ok / unwrap_error for the two Result arms,
and unwrap_some as an explicit synonym for unwrap. Here the
canonical Result extractor is unwrap_ok; a bare unwrap is kept
as a synonym on Result for call-site brevity.
Examples
use Std.Result
let positive = fn(x: Int) -> Result(Int, Atom) =
pickup
x > 0 -> ok(x)
else -> error(:neg)
ok(42)
|> map(fn(x) -> x * 2)
|> and_then(positive)
|> unwrap_ok(0) # => 84use 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)
Summary
Types
Group tag consumed by Cure.Stdlib.Preload.
A computation that either succeeded with a value (Ok) or failed with a
reason (Error). Ok(v) erases to {:ok, v} and Error(e) to
{:error, e} on the BEAM — the OTP-conventional shape Erlang, Elixir,
and AtomVM FFI expect.
Functions
Monadic bind on Ok; short-circuits on Error.
Build an Error(reason) Result.
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.
true when the result is in the Error arm.
true when the result is in the Ok arm.
Functorial map over the Ok arm; passes Error through.
Functorial map over the Error arm; passes Ok through.
Build an Ok(value) Result.
Recovery: continue with f(reason) on Error; pass Ok through.
Synonym for unwrap_ok, kept so call sites can read as a bare
unwrap on a Result. Same behaviour, same default.
Extract the Error reason or fall back to default on Ok.
Extract the Ok value or fall back to default on Error.
The canonical Result extractor name; unwrap below is a synonym.
Types
Functions
fn and_then(result: Result(t, e), f: Function(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: Function(t, u)) -> Result(u, e)
Functorial map over the Ok arm; passes Error through.
fn map_error(result: Result(t, e), f: Function(e, b)) -> Result(t, b)
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: Function(e, Result(t, b))) -> Result(t, b)
Recovery: continue with f(reason) on Error; pass Ok through.
fn unwrap(result: Result(t, e), default: t) -> t
Synonym for unwrap_ok, kept so call sites can read as a bare
unwrap on a Result. Same behaviour, same default.
fn unwrap_error(result: Result(t, e), default: e) -> e
Extract the Error reason or fall back to default on Ok.
fn unwrap_ok(result: Result(t, e), default: t) -> t
Extract the Ok value or fall back to default on Error.
The canonical Result extractor name; unwrap below is a synonym.