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

cure
use Std.Option

map(Some(3), fn(x) -> x * 2)            # => Some(6)
unwrap(None(), 0)                       # => 0
filter(Some(3), fn(x) -> x > 5)         # => None()
cure
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 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 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.