Lex
Menu
On this page

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.

FamilyUnitsExact conversions
Byte sizesbytes, KiB, pages, MiB1 KiB = 1024 bytes, 1 pages = 4096 bytes, 1 MiB = 1024 KiB
Block countblocksCompatible only with blocks
Durationsns, us, ms, s1 us = 1000 ns, 1 ms = 1000 us, 1 s = 1000 ms
{
  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

FunctionSignatureBehavior
mapmap(list, fn)Applies a 1-parameter function to each item in order.
filterfilter(list, pred)Keeps items where boolean predicate pred returns true.
foldfold(list, init, fn)Iterative left fold fn(acc, item) in O(1) stack space.
zipzip(left, right)Pairs elements as { first, second } up to the shorter length.
lenlen(list_or_text)Returns item count of a List or UTF-8 Unicode code point count of a Text.
atat(list, index)Returns the 0-based item; refuses negative, out-of-bounds, or unit indices.
sliceslice(list, start, end)Returns the sub-list [start, end); checks bounds strictly.
anyany(list, pred)Short-circuiting existential boolean test.
allall(list, pred)Short-circuiting universal boolean test.
flat_mapflat_map(list, fn)Maps each item to a List and flattens in one pass.
uniqueunique(list)Deduplicates scalar items preserving first-seen order.
sort_bysort_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

FunctionSignatureBehavior
fieldsfields(rec)Returns field names in canonical ascending UTF-8 order without forcing values.
valuesvalues(rec)Forces and returns field values in canonical ascending key order.
hashas(rec, name)Checks whether name exists in rec without forcing any field.
extendextend(a, b)Lazily adds and overrides fields from b onto a.
merge_deepmerge_deep(a, b)Recursively merges nested records lazily within 64 nesting levels.
from_entriesfrom_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

FunctionSignatureBehavior
splitsplit(text, sep)Splits text by non-empty sep into a List of Text.
join_textjoin_text(list, sep)Joins a List of Text with sep.
starts_withstarts_with(text, prefix)Tests whether text begins with prefix.
ends_withends_with(text, suffix)Tests whether text ends with suffix.
containscontains(text, needle)Tests whether text contains substring needle.
replace_textreplace_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

let port = 5010 in assert(port > 0, "port must be positive", port)
=>5010