Module
pooka/option
src/pooka/option.pookaSometimes the envelope is simply empty.
Transform Some values while leaving None alone. Fallback callbacks are lazy, and flat_map avoids nesting another Option around a result that is already optional.
import pooka/option
assert option.map(Some(2), fn(x) { x + 1 }) == Some(3)
API reference
map(value: Option(a), transform: fn(a) -> b) -> Option(b)
INFERRED
fn(Option(a), fn(a) -> b) -> Option(b)Transform the carried value; an absent or failed branch passes through unchanged.
import pooka/option
assert option.map(Some(2), fn(x) { x + 1 }) == Some(3)
flat_map(value: Option(a), transform: fn(a) -> Option(b)) -> Option(b)
INFERRED
fn(Option(a), fn(a) -> Option(b)) -> Option(b)Chain a callback that already returns the same fallible wrapper.
import pooka/option
assert option.flat_map(Some(2), fn(x) { Some(x + 1) }) == Some(3)
unwrap_or(value: Option(a), default: a) -> a
INFERRED
fn(Option(a), a) -> aReturn the carried value, or the already evaluated fallback.
import pooka/option
assert option.unwrap_or(None, 7) == 7
unwrap_or_else(value: Option(a), fallback: fn() -> a) -> a
INFERRED
fn(Option(a), fn() -> a) -> aCall the fallback only for None. A Some value leaves it asleep.
import pooka/option
assert option.unwrap_or_else(None, fn() { 7 }) == 7
is_some(value: Option(a)) -> Bool
INFERRED
fn(Option(a)) -> BoolAsk whether an optional value is present.
import pooka/option
assert option.is_some(Some(1))
is_none(value: Option(a)) -> Bool
INFERRED
fn(Option(a)) -> BoolAsk whether an optional value is absent.
import pooka/option
assert option.is_none(None)
to_result(value: Option(a), error: e) -> Result(a, e)
INFERRED
fn(Option(a), b) -> Result(a, b)Replace absence with the supplied error value.
import pooka/option
assert option.to_result(Some(1), "missing") == Ok(1)