Std.Match (Cure Standard Library v0.37.2)

View Source

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.

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. The helpers below follow it: unwrap_ok, unwrap_error, and unwrap_some.

Examples

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]])
use Std.Match

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

Summary

Functions

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

First component of a pair.

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

Second component of a pair.

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

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

Extract the reason from an Error; falls back to default on Ok. Mirror of unwrap_ok for the error arm.

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

Extract the value from a Some; falls back to default on None. Synonym for Std.Option.unwrap, named to mirror unwrap_ok.

Functions

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

 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.

fst(t: Tuple(a, b)) -> a

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

First component of a pair.

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

 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.

snd(t: Tuple(a, b)) -> b

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

Second component of a pair.

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

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

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

unpack_pair(t: Tuple(a, b)) -> Tuple(a, b)

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

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

unwrap_error(r: Result(t, e), default: e) -> e

 fn unwrap_error(r: Result(t, e), default: e) -> e

Extract the reason from an Error; falls back to default on Ok. Mirror of unwrap_ok for the error arm.

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

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

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

unwrap_some(o: Option(a), default: a) -> a

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

Extract the value from a Some; falls back to default on None. Synonym for Std.Option.unwrap, named to mirror unwrap_ok.