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. Option(T) lowers to {:some, v} / {:none} on the BEAM.

Examples

use Std.Option

some(3)                                 # => Some(3)
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()

Functions

  • # fn __group__() -> Atom

    Group tag consumed by Cure.Stdlib.Preload.

  • # fn filter(opt: Option(T), pred: (T) -> Bool) -> Option(T)

    Keep the value only when pred accepts it; otherwise drop to None.

  • # fn flat_map(opt: Option(T), f: (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: (T) -> U) -> Option(U)

    Functorial map; preserves None.

  • # fn none() -> Option(T)

    The empty Option.

  • # fn or_else(opt: Option(T), default: T) -> T

    Unwrap-or-default. Useful as a pipeline terminal.

  • # fn some(value: T) -> Option(T)

    Build a Some(value) Option.

  • # 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)

    Pair two Options. Returns Some(%[va, vb]) when both sides are Some, None otherwise.