Std.Match
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
unwrap, head/tail, etc.
Examples
use Std.Match
fst(%[42, "x"]) # => 42
head_tail([1, 2, 3], 0) # => %[1, [2, 3]]
head_tail([], 0) # => %[0, []]
first_two([1, 2, 3, 4], 0) # => %[1, 2, [3, 4]]
use Std.Match
unwrap_ok(Ok(42), 0) # => 42
unwrap_some(Some("ok"), "fallback") # => "ok"
unwrap_some(None(), "fallback") # => "fallback"
Functions
-
# fn __group__() -> Atom
Group tag consumed by
Cure.Stdlib.Preload. -
# fn first_two(list: List(T), default: T) -> Tuple
Extract the first two elements of
listas%[h1, h2, rest], padding withdefaultwhen there are fewer. -
# fn fst(t: Tuple) -> T
First element of a tuple; handles pairs and singletons.
-
# fn head_tail(list: List(T), default: T) -> Tuple
Split
listinto%[head, tail], falling back to%[default, []]for an empty list. -
# fn snd(t: Tuple) -> T
Second element of a tuple; handles pairs and triples (returning the middle entry for the latter).
-
# fn uncons(list: List(T)) -> Tuple
%[head, tail]for non-empty lists;%[[], []]for empty. -
# fn unpack_pair(t: Tuple) -> Tuple
Return a pair unchanged when it destructures as
%[a, b]; otherwise return the argument as-is. -
# fn unwrap_ok(r: Result(T, E), default: T) -> T
Extract the value from an
Ok; falls back todefaultonError. -
# fn unwrap_some(o: Option(T), default: T) -> T
Extract the value from a
Some; falls back todefaultonNone.