Std. Option
(Cure Standard Library v0.37.2)
View Source
Standalone Option(T) helpers.
Parallel to the Option half of Std.Core, kept separate so that
callers can use Std.Option without pulling in the rest of the
core combinators. Some(v) lowers to {:some, v} and the nullary
None() to the bare atom :none on the BEAM — the OTP-conventional
shape Erlang, Elixir, and AtomVM FFI expect.
Extractor naming
The stdlib uses one extractor convention: unwrap for the Option
happy path, unwrap_ok / unwrap_error for the two Result arms,
and unwrap_some as an explicit synonym for unwrap when a call site
wants to name the Some arm for symmetry with unwrap_ok. All four
take the value and a default, and never raise.
Examples
use Std.Option
map(Some(3), fn(x) -> x * 2) # => Some(6)
unwrap(None(), 0) # => 0
filter(Some(3), fn(x) -> x > 5) # => None()use Std.Option
# Chain two Options into one.
zip(Some("Ada"), Some(1815)) # => Some(%["Ada", 1815])
zip(Some("Ada"), None()) # => None()
Summary
Types
Group tag consumed by Cure.Stdlib.Preload.
An optional value: either present (Some) or absent (None). Some(v)
erases to {:some, v} and the nullary None() to the bare atom :none
on the BEAM.
Functions
Keep the value only when pred accepts it; otherwise drop to
None.
Monadic bind; short-circuits on None.
true when the option is empty.
true when the option holds a value.
Functorial map; preserves None.
Unwrap-or-default. Useful as a pipeline terminal.
Extract the value; returns default on None.
Explicit synonym for unwrap that names the Some arm, for symmetry
with unwrap_ok/unwrap_error on Result. Returns default on None.
Pair two Options. Returns Some(%[va, vb]) when both sides are
Some, None otherwise.
Types
Functions
fn filter(opt: Option(t), pred: Function(t, Bool)) -> Option(t)
Keep the value only when pred accepts it; otherwise drop to
None.
fn flat_map(opt: Option(t), f: Function(t, Option(u))) -> Option(u)
Monadic bind; short-circuits on None.
fn is_none(opt: Option(t)) -> Bool
true when the option is empty.
fn is_some(opt: Option(t)) -> Bool
true when the option holds a value.
fn map(opt: Option(t), f: Function(t, u)) -> Option(u)
Functorial map; preserves None.
fn or_else(opt: Option(t), default: t) -> t
Unwrap-or-default. Useful as a pipeline terminal.
fn unwrap(opt: Option(t), default: t) -> t
Extract the value; returns default on None.
fn unwrap_some(opt: Option(t), default: t) -> t
Explicit synonym for unwrap that names the Some arm, for symmetry
with unwrap_ok/unwrap_error on Result. Returns default on None.
fn zip(a: Option(t), b: Option(u)) -> Option(Tuple(t, u))
Pair two Options. Returns Some(%[va, vb]) when both sides are
Some, None otherwise.