Standard Library & Units
Lex provides checked physical units on integer literals and 26 pure built-in functions that work identically in console mode and declaration mode.
Physical units
An integer literal followed on the same line by one of nine unit names carries that unit inside the value. Arithmetic converts compatible units within one family to the finer unit with exact overflow-checked math and zero silent truncation.
| Family | Units | Exact conversions |
|---|---|---|
| Byte sizes | bytes, KiB, pages, MiB | 1 KiB = 1024 bytes, 1 pages = 4096 bytes, 1 MiB = 1024 KiB |
| Block count | blocks | Compatible only with blocks |
| Durations | ns, us, ms, s | 1 us = 1000 ns, 1 ms = 1000 us, 1 s = 1000 ms |
+,-, and comparisons accept identical or compatible units and return the finer unit.*acceptsunit * scalarorscalar * unitand refusesunit * unit./acceptsunit / scalar(preserving the unit) orunit / compatible_unit(producing a dimensionlessInteger).- Mixing incompatible families (
64 MiB + 1,1 MiB + 1 ms,1 pages + 1 blocks) stops withUnit_Mismatch.
{
budget = 1 MiB + 512 KiB,
remaining = 1 s - 250 ms,
ratio = 16 KiB / 1 pages,
}
=>
{ budget = 1536 KiB, ratio = 4, remaining = 750 ms }List functions
| Function | Signature | Behavior |
|---|---|---|
map | map(list, fn) | Applies a 1-parameter function to each item in order. |
filter | filter(list, pred) | Keeps items where boolean predicate pred returns true. |
fold | fold(list, init, fn) | Iterative left fold fn(acc, item) in O(1) stack space. |
zip | zip(left, right) | Pairs elements as { first, second } up to the shorter length. |
len | len(list_or_text) | Returns item count of a List or UTF-8 Unicode code point count of a Text. |
at | at(list, index) | Returns the 0-based item; refuses negative, out-of-bounds, or unit indices. |
slice | slice(list, start, end) | Returns the sub-list [start, end); checks bounds strictly. |
any | any(list, pred) | Short-circuiting existential boolean test. |
all | all(list, pred) | Short-circuiting universal boolean test. |
flat_map | flat_map(list, fn) | Maps each item to a List and flattens in one pass. |
unique | unique(list) | Deduplicates scalar items preserving first-seen order. |
sort_by | sort_by(list, key_fn) | Stable ascending merge sort by unit-compatible Integer or Text key. |
{
sums = zip([10, 20], [1, 2]) |> map(({ first, second }) => first + second),
flat = flat_map([1, 2], (x) => [x, x * 10]),
dedup = unique([1, 2, 1, 3, 2]),
sorted = sort_by(["c", "a", "b"], (s) => s),
}
=>
{ dedup = [1, 2, 3], flat = [1, 10, 2, 20], sorted = ["a", "b", "c"], sums = [11, 22] }Record functions
| Function | Signature | Behavior |
|---|---|---|
fields | fields(rec) | Returns field names in canonical ascending UTF-8 order without forcing values. |
values | values(rec) | Forces and returns field values in canonical ascending key order. |
has | has(rec, name) | Checks whether name exists in rec without forcing any field. |
extend | extend(a, b) | Lazily adds and overrides fields from b onto a. |
merge_deep | merge_deep(a, b) | Recursively merges nested records lazily within 64 nesting levels. |
from_entries | from_entries(list) | Builds a canonical record from [{ name = Text, value = any }], refusing duplicates. |
let
r = { b = 1 / 0, a = 1 }
in
{
keys = fields(r),
has_a = has(r, "a"),
merged = merge_deep({ net = { port = 80 } }, { net = { host = "h" } }),
}
=>
{ has_a = true, keys = ["a", "b"], merged = { net = { host = "h", port = 80 } } }Text functions
| Function | Signature | Behavior |
|---|---|---|
split | split(text, sep) | Splits text by non-empty sep into a List of Text. |
join_text | join_text(list, sep) | Joins a List of Text with sep. |
starts_with | starts_with(text, prefix) | Tests whether text begins with prefix. |
ends_with | ends_with(text, suffix) | Tests whether text ends with suffix. |
contains | contains(text, needle) | Tests whether text contains substring needle. |
replace_text | replace_text(text, old, new) | Replaces every non-overlapping occurrence of non-empty old with new. |
{
codepoints = len("café"),
joined = split("a,b,c", ",") |> join_text("-"),
replaced = replace_text("foo bar foo", "foo", "baz"),
}
=>
{ codepoints = 4, joined = "a-b-c", replaced = "baz bar baz" }Assertions and tracing
assert(cond, msg, value)checks booleancond; onfalseit forcesTextmsgand stops withAssertion_Failed; ontrueit skipsmsgand returnsvaluein tail position.trace(msg, value)appendsTextmsgto the caller-supplied boundedTrace_Logwith zero ambient I/O and returnsvaluein tail position.
let port = 5010 in assert(port > 0, "port must be positive", port)
=>
5010