Module
pooka/result
src/pooka/result.pookaCarry the answer and the explanation in different envelopes.
Transform successful values or errors without throwing away the other branch. flat_map chains a fallible callback, while conversion to Option deliberately discards error details.
import pooka/result
assert result.map(Ok(2), fn(x) { x + 1 }) == Ok(3)
API reference
map(value: Result(a, e), transform: fn(a) -> b) -> Result(b, e)
INFERRED
fn(Result(a, b), fn(a) -> c) -> Result(c, b)Transform the carried value; an absent or failed branch passes through unchanged.
import pooka/result
assert result.map(Ok(2), fn(x) { x + 1 }) == Ok(3)
map_error(value: Result(a, e), transform: fn(e) -> f) -> Result(a, f)
INFERRED
fn(Result(a, b), fn(b) -> c) -> Result(a, c)Transform only the error, preserving successful values.
import pooka/result
assert result.map_error(Error(2), fn(x) { x + 1 }) == Error(3)
flat_map(value: Result(a, e), transform: fn(a) -> Result(b, e)) -> Result(b, e)
INFERRED
fn(Result(a, b), fn(a) -> Result(c, b)) -> Result(c, b)Chain a callback that already returns the same fallible wrapper.
import pooka/result
assert result.flat_map(Ok(2), fn(x) { Ok(x + 1) }) == Ok(3)
unwrap_or(value: Result(a, e), default: a) -> a
INFERRED
fn(Result(a, b), a) -> aReturn the carried value, or the already evaluated fallback.
import pooka/result
assert result.unwrap_or(Error("missing"), 7) == 7
unwrap_or_else(value: Result(a, e), fallback: fn(e) -> a) -> a
INFERRED
fn(Result(a, b), fn(b) -> a) -> aCall the fallback with the error only when the Result failed.
import pooka/result
assert result.unwrap_or_else(Error(2), fn(error) { error + 1 }) == 3
is_ok(value: Result(a, e)) -> Bool
INFERRED
fn(Result(a, b)) -> BoolAsk whether the Result carries success.
import pooka/result
assert result.is_ok(Ok(1))
is_error(value: Result(a, e)) -> Bool
INFERRED
fn(Result(a, b)) -> BoolAsk whether the Result carries an error.
import pooka/result
assert result.is_error(Error("missing"))
to_option(value: Result(a, e)) -> Option(a)
INFERRED
fn(Result(a, b)) -> Option(a)Keep a successful value and deliberately discard error details.
import pooka/result
assert result.to_option(Ok(1)) == Some(1)