Std.Result
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}.
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 onError. -
# 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)intoResult(T, E). Useful whenand_thenproduced anotherResultand you want the inner arm directly. -
# fn is_error(result: Result(T, E)) -> Bool
truewhen the result is in theErrorarm. -
# fn is_ok(result: Result(T, E)) -> Bool
truewhen the result is in theOkarm. -
# fn map(result: Result(T, E), f: (T) -> U) -> Result(U, E)
Functorial map over the
Okarm; passesErrorthrough. -
# fn map_error(result: Result(T, E), f: (E) -> F) -> Result(T, F)
Functorial map over the
Errorarm; passesOkthrough. -
# 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)onError; passOkthrough. -
# fn unwrap(result: Result(T, E), default: T) -> T
Extract the
Okvalue or fall back todefaultonError. -
# fn unwrap_error(result: Result(T, E), default: E) -> E
Extract the
Errorreason or fall back todefaultonOk.