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)
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)
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
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
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
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
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)
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)
all(values: List(Option(a))) -> Option(List(a))
fn(List(Option(a))) -> Option(List(a))Open every envelope; one absence makes the whole answer absent.
assert option.all([Some(1), Some(2)]) == Some([1, 2])
flatten(value: Option(Option(a))) -> Option(a)
fn(Option(Option(a))) -> Option(a)Remove one optional layer without inventing a missing value.
assert option.flatten(Some(Some(1))) == Some(1)
or_else(value: Option(a), fallback: fn() -> Option(a)) -> Option(a)
fn(Option(a), fn() -> Option(a)) -> Option(a)Ask for another envelope only when the first one is empty.
assert option.or_else(None, fn() { Some(1) }) == Some(1)
values(items: List(Option(a))) -> List(a)
fn(List(Option(a))) -> List(a)Keep present values in their original order.
assert option.values([Some(1), None, Some(2)]) == [1, 2]