Convenience helpers built on top of v0.18.0 deep destructuring.

Every function here is implemented with nested patterns and serves as a smoke test for the pattern engine. They also double as lightweight convenience utilities when you want a single-call fst/snd, uncons, etc.

Tuples are the parameterised Tuple(...) of Std.Tuple: a pair is Tuple(a, b), so fst/snd are total projections at their true component types. List splitters that can fail on a short list return an Option rather than a padded tuple, matching the Haskell-style uncons.

Examples

cure
use Std.Match

fst(%[42, "x"])                         # => 42
uncons([1, 2, 3])                       # => Some(%[1, [2, 3]])
uncons([])                              # => None()
first_two([1, 2, 3, 4])                 # => Some(%[1, 2, [3, 4]])
cure
use Std.Match

unwrap_ok(Ok(42), 0)                    # => 42
unwrap_some(Some("ok"), "fallback")     # => "ok"
unwrap_some(None(), "fallback")         # => "fallback"

Functions

  • # fn first_two(list: List(a)) -> Option(Tuple(a, a, List(a)))

    Some(%[h1, h2, rest]) with the first two elements of list and the remainder; None() when the list has fewer than two.

  • # fn fst(t: Tuple(a, b)) -> a

    First component of a pair.

  • # fn head_tail(list: List(a), default: a) -> Tuple(a, List(a))

    Split list into %[head, tail], falling back to %[default, []] for an empty list.

  • # fn snd(t: Tuple(a, b)) -> b

    Second component of a pair.

  • # fn uncons(list: List(a)) -> Option(Tuple(a, List(a)))

    Some(%[head, tail]) for a non-empty list; None() when empty.

  • # fn unpack_pair(t: Tuple(a, b)) -> Tuple(a, b)

    Return a pair unchanged (identity destructure of a Tuple(a, b)).

  • # fn unwrap_ok(r: Result(a, e), default: a) -> a

    Extract the value from an Ok; falls back to default on Error.

  • # fn unwrap_some(o: Option(a), default: a) -> a

    Extract the value from a Some; falls back to default on None.