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)
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)
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)
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
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
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
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
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)
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)
all(values: List(Result(a, e))) -> Result(List(a), e)
fn(List(Result(a, b))) -> Result(List(a), b)Open every successful envelope, stopping at the first error.
assert result.all([Ok(1), Ok(2)]) == Ok([1, 2])
flatten(value: Result(Result(a, e), e)) -> Result(a, e)
fn(Result(Result(a, b), b)) -> Result(a, b)Remove one layer of wrapping; either error keeps its explanation.
assert result.flatten(Ok(Ok(1))) == Ok(1)
recover(value: Result(a, e), fallback: fn(e) -> Result(a, f)) -> Result(a, f)
fn(Result(a, b), fn(b) -> Result(a, c)) -> Result(a, c)Ask the fallback only after failure. It may change the error type.
assert result.recover(Error("missing"), fn(_) { Ok(1) }) == Ok(1)
partition(values: List(Result(a, e))) -> #(List(a), List(e))
fn(List(Result(a, b))) -> #(List(a), List(b))Separate successes and errors, preserving their input order.
assert result.partition([Ok(1), Error("no")]) == #([1], ["no"])
values(items: List(Result(a, e))) -> List(a)
fn(List(Result(a, b))) -> List(a)Keep successful values in order, deliberately leaving errors behind.
assert result.values([Ok(1), Error("no"), Ok(2)]) == [1, 2]