Std.Option
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.
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()
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.
Types
-
type Option = Some | None
Functions
-
# fn filter(opt: Option(t), pred: Function(t, Bool)) -> Option(t)
Keep the value only when
predaccepts it; otherwise drop toNone. -
# 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
truewhen the option is empty. -
# fn is_some(opt: Option(t)) -> Bool
truewhen 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
defaultonNone. -
# fn zip(a: Option(t), b: Option(u)) -> Option(Tuple(t, u))
Pair two Options. Returns
Some(%[va, vb])when both sides areSome,Noneotherwise.