Module

pooka/result

src/pooka/result.pooka

Carry 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.

Pooka
import pooka/result

assert result.map(Ok(2), fn(x) { x + 1 }) == Ok(3)

API reference

function

map

src/pooka/result.pooka:27
map(value: Result(a, e), transform: fn(a) -> b) -> Result(b, e)
INFERREDfn(Result(a, b), fn(a) -> c) -> Result(c, b)

Transform the carried value; an absent or failed branch passes through unchanged.

Pooka
import pooka/result

assert result.map(Ok(2), fn(x) { x + 1 }) == Ok(3)
function

map_error

src/pooka/result.pooka:41
map_error(value: Result(a, e), transform: fn(e) -> f) -> Result(a, f)
INFERREDfn(Result(a, b), fn(b) -> c) -> Result(a, c)

Transform only the error, preserving successful values.

Pooka
import pooka/result

assert result.map_error(Error(2), fn(x) { x + 1 }) == Error(3)
function

flat_map

src/pooka/result.pooka:55
flat_map(value: Result(a, e), transform: fn(a) -> Result(b, e)) -> Result(b, e)
INFERREDfn(Result(a, b), fn(a) -> Result(c, b)) -> Result(c, b)

Chain a callback that already returns the same fallible wrapper.

Pooka
import pooka/result

assert result.flat_map(Ok(2), fn(x) { Ok(x + 1) }) == Ok(3)
function

unwrap_or

src/pooka/result.pooka:69
unwrap_or(value: Result(a, e), default: a) -> a
INFERREDfn(Result(a, b), a) -> a

Return the carried value, or the already evaluated fallback.

Pooka
import pooka/result

assert result.unwrap_or(Error("missing"), 7) == 7
function

unwrap_or_else

src/pooka/result.pooka:83
unwrap_or_else(value: Result(a, e), fallback: fn(e) -> a) -> a
INFERREDfn(Result(a, b), fn(b) -> a) -> a

Call the fallback with the error only when the Result failed.

Pooka
import pooka/result

assert result.unwrap_or_else(Error(2), fn(error) { error + 1 }) == 3
function

is_ok

src/pooka/result.pooka:97
is_ok(value: Result(a, e)) -> Bool
INFERREDfn(Result(a, b)) -> Bool

Ask whether the Result carries success.

Pooka
import pooka/result

assert result.is_ok(Ok(1))
function

is_error

src/pooka/result.pooka:111
is_error(value: Result(a, e)) -> Bool
INFERREDfn(Result(a, b)) -> Bool

Ask whether the Result carries an error.

Pooka
import pooka/result

assert result.is_error(Error("missing"))
function

to_option

src/pooka/result.pooka:122
to_option(value: Result(a, e)) -> Option(a)
INFERREDfn(Result(a, b)) -> Option(a)

Keep a successful value and deliberately discard error details.

Pooka
import pooka/result

assert result.to_option(Ok(1)) == Some(1)
function

all

src/pooka/result.pooka:134
all(values: List(Result(a, e))) -> Result(List(a), e)
INFERREDfn(List(Result(a, b))) -> Result(List(a), b)

Open every successful envelope, stopping at the first error.

Pooka
assert result.all([Ok(1), Ok(2)]) == Ok([1, 2])
function

flatten

src/pooka/result.pooka:151
flatten(value: Result(Result(a, e), e)) -> Result(a, e)
INFERREDfn(Result(Result(a, b), b)) -> Result(a, b)

Remove one layer of wrapping; either error keeps its explanation.

Pooka
assert result.flatten(Ok(Ok(1))) == Ok(1)
function

recover

src/pooka/result.pooka:160
recover(value: Result(a, e), fallback: fn(e) -> Result(a, f)) -> Result(a, f)
INFERREDfn(Result(a, b), fn(b) -> Result(a, c)) -> Result(a, c)

Ask the fallback only after failure. It may change the error type.

Pooka
assert result.recover(Error("missing"), fn(_) { Ok(1) }) == Ok(1)
function

partition

src/pooka/result.pooka:172
partition(values: List(Result(a, e))) -> #(List(a), List(e))
INFERREDfn(List(Result(a, b))) -> #(List(a), List(b))

Separate successes and errors, preserving their input order.

Pooka
assert result.partition([Ok(1), Error("no")]) == #([1], ["no"])
function

values

src/pooka/result.pooka:188
values(items: List(Result(a, e))) -> List(a)
INFERREDfn(List(Result(a, b))) -> List(a)

Keep successful values in order, deliberately leaving errors behind.

Pooka
assert result.values([Ok(1), Error("no"), Ok(2)]) == [1, 2]