Builtin Functions
Every built-in function in WCL — its signature, parameters, return value, and a usage example. Reflection and meta built-ins are introspected with fn_signature.
Index
- abs
- acos
- asin
- assert
- ast_string
- at
- atan
- atan2
- builtin_names
- cbrt
- ceil
- child_types
- clamp
- concat
- contains
- cos
- decorator_arg
- decorator_names
- degrees
- drop
- e
- ends_with
- error
- eval
- exp
- filter
- flatten
- floor
- fn_signature
- fold
- format
- head
- hypot
- index_of
- join
- len
- list_contains
- ln
- log10
- log2
- map
- max
- min
- panic
- pi
- pow
- radians
- range
- replace
- reverse
- round
- sign
- sin
- sort
- sort_connected
- split
- sqrt
- starts_with
- sum
- tail
- take
- tan
- tau
- tensor
- tensor_data
- tensor_reshape
- tensor_shape
- to_lower
- to_upper
- trim
- trunc
- type_fields
- unique
- zip
Functions
abs
abs(x: number) -> f64Absolute value.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
abs(-5) // 5.0
acos
acos(x: number) -> f64Arccosine, in radians, of a value in [-1, 1].
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
acos(1) // 0.0
asin
asin(x: number) -> f64Arcsine, in radians, of a value in [-1, 1].
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
asin(1) // 1.5707963… (π/2)
assert
assert(cond: bool, msg: utf8) -> noneReturn none when cond is true, otherwise abort with msg.
| Parameter | Type | Description |
|---|---|---|
| cond | bool | The condition that must hold. |
| msg | utf8 | The error message reported when cond is false. |
| Returns | none | none when the assertion holds (otherwise aborts). |
Example
assert(len(xs) > 0, "list must not be empty")
ast_string
ast_string(target: &T) -> utf8Pretty-print the canonical source behind a reference (type/interface/union/symbol_set/block/field) or a function value.
| Parameter | Type | Description |
|---|---|---|
| target | &T | A dataref to a declaration, or a function value. |
| Returns | utf8 | The canonical (pretty-printed) source text. |
Example
ast_string(Image) // the Image type's source, pretty-printed
at
at(xs: [T], i: i64) -> TThe element at a zero-based index; errors if out of bounds or negative.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list to index. |
| i | i64 | The zero-based index. |
| Returns | T | The element at i. |
Example
at([10, 20, 30], 1) // 20
atan
atan(x: number) -> f64Arctangent, in radians.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
atan(1) // 0.7853981… (π/4)
atan2
atan2(a: number, b: number) -> f64Arctangent of a/b in radians, using the signs of both to pick the quadrant.
| Parameter | Type | Description |
|---|---|---|
| a | number | The first operand. |
| b | number | The second operand. |
| Returns | f64 | The result, as an f64. |
Example
atan2(1, 1) // 0.7853981… (π/4)
builtin_names
builtin_names() -> [utf8]The names of every registered built-in function, sorted. Pair with fn_signature to introspect each one.
| Parameter | Type | Description |
|---|---|---|
| Returns | [utf8] | Every built-in's name, sorted alphabetically. |
Example
builtin_names() // ["abs", "acos", …, "zip"]
cbrt
cbrt(x: number) -> f64Cube root.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
cbrt(27) // 3.0
ceil
ceil(x: number) -> f64Round up to the nearest integer.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
ceil(2.1) // 3.0
child_types
child_types(target: &T) -> [&T]Reflect a type into references to the element types of its @child / @children block slots (own slots first, then inherited via extends). Pair with type_table / type_fields to auto-document the blocks a @document declares.
| Parameter | Type | Description |
|---|---|---|
| target | &T | A reference to a type or interface declaration. |
| Returns | [&T] | One type reference per block slot. Slots that accept a union or interface resolve to that type's name; scalar (non-block) fields are skipped. |
Example
child_types(MyDoc) // [&ProjectMeta, &Settings] — the element types of MyDoc's block slots
clamp
clamp(x: number, lo: number, hi: number) -> f64Constrain x to the range [lo, hi].
| Parameter | Type | Description |
|---|---|---|
| x | number | The value to clamp. |
| lo | number | The lower bound. |
| hi | number | The upper bound. |
| Returns | f64 | x limited to [lo, hi], as an f64. |
Example
clamp(12, 0, 10) // 10.0
concat
concat(a: utf8, b: utf8) -> utf8Concatenate two strings into one.
| Parameter | Type | Description |
|---|---|---|
| a | utf8 | The left-hand string. |
| b | utf8 | The string appended after a. |
| Returns | utf8 | The two strings joined together. |
Example
concat("foo", "bar") // "foobar"
contains
contains(s: utf8, needle: utf8) -> boolWhether a string contains a substring.
| Parameter | Type | Description |
|---|---|---|
| s | utf8 | The string to search. |
| needle | utf8 | The substring to look for. |
| Returns | bool | true if the substring is present. |
Example
contains("hello", "ell") // true
cos
cos(x: number) -> f64Cosine of an angle in radians.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
cos(0) // 1.0
decorator_arg
decorator_arg(target: &T, decorator: utf8, slot: utf8) -> anyRead one named argument of a decorator on a referenced declaration (none if absent).
| Parameter | Type | Description |
|---|---|---|
| target | &T | A reference to a type, field, block, or variant. |
| decorator | utf8 | The decorator name, e.g. "doc". |
| slot | utf8 | The argument (slot) name to read. |
| Returns | any | The argument's value, or none if absent. |
Example
decorator_arg(Image, "block", "name") // "image"
decorator_names
decorator_names(target: &T) -> [utf8]List the names of the decorators attached to a referenced declaration.
| Parameter | Type | Description |
|---|---|---|
| target | &T | A reference to a type, field, block, or variant. |
| Returns | [utf8] | The decorator names, in source order. |
Example
decorator_names(Image) // ["block", "schemaless", …]
degrees
degrees(x: number) -> f64Convert an angle from radians to degrees.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
degrees(pi()) // 180.0
drop
drop(xs: [T], n: i64) -> [T]Every element of a list after the first n.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list to drop from. |
| n | i64 | How many leading elements to skip. |
| Returns | [T] | The elements after the first n. |
Example
drop([1, 2, 3, 4], 2) // [3, 4]
e
e() -> f64Euler's number e (≈ 2.71828).
| Parameter | Type | Description |
|---|---|---|
| Returns | f64 | The value of e. |
Example
e() // 2.71828…
ends_with
ends_with(s: utf8, suffix: utf8) -> boolWhether a string ends with a suffix.
| Parameter | Type | Description |
|---|---|---|
| s | utf8 | The string to test. |
| suffix | utf8 | The suffix to look for. |
| Returns | bool | true if the string ends with the suffix. |
Example
ends_with("hello", "lo") // true
error
error(msg: utf8) -> neverAbort evaluation with an error message.
| Parameter | Type | Description |
|---|---|---|
| msg | utf8 | The error message to report. |
| Returns | never | Never returns — aborts evaluation. |
Example
if found { value } else { error("config not found") }
eval
eval(src: utf8) -> anyParse a string as a WCL expression and evaluate it in the current scope.
| Parameter | Type | Description |
|---|---|---|
| src | utf8 | WCL expression source to parse and evaluate. |
| Returns | any | The value the expression evaluates to. |
Example
eval("1 + 2 * 3") // 7
exp
exp(x: number) -> f64e raised to the power x.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
exp(1) // 2.71828…
filter
filter(xs: [T], pred: fn (T) -> bool) -> [T]Keep only the list elements for which the predicate returns true.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list to filter. |
| pred | fn (T) -> bool | Predicate deciding whether to keep an element. |
| Returns | [T] | The elements for which the predicate returned true. |
Example
filter(range(0, 6), fn(x: i64) -> bool x % 2 == 0) // [0, 2, 4]
flatten
flatten(xss: [[T]]) -> [T]Concatenate a list of lists into a single list, one level deep.
| Parameter | Type | Description |
|---|---|---|
| xss | [[T]] | A list whose elements are themselves lists. |
| Returns | [T] | The inner lists concatenated, one level deep. |
Example
flatten([[1, 2], [3], [4, 5]]) // [1, 2, 3, 4, 5]
floor
floor(x: number) -> f64Round down to the nearest integer.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
floor(2.7) // 2.0
fn_signature
fn_signature(f: any) -> recordDescribe a function's parameters and return type. Pass a function value, or a built-in's name as a string.
| Parameter | Type | Description |
|---|---|---|
| f | any | A function value, or the name of a built-in as a utf8 string. |
| Returns | record | A record { doc, params: [{name, type, doc}], returntype, returndoc, signature, is_builtin }. |
Example
fn_signature("map") // { signature: "fn(xs: [T], …) -> [U]", params: […], … }
fold
fold(xs: [T], init: U, f: fn (U, T) -> U) -> UReduce a list or tensor to a single value by repeatedly combining the accumulator with each element.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list or tensor to reduce. |
| init | U | The initial accumulator value. |
| f | fn (U, T) -> U | Combines the accumulator with the next element. |
| Returns | U | The final accumulator value. |
Example
fold([1, 2, 3, 4], 0, fn(acc: i64, x: i64) -> i64 acc + x) // 10
format
format(utf8, ...args) -> utf8Substitute trailing arguments into a template's {} placeholders ({{/}} are literal braces).
| Parameter | Type | Description |
|---|---|---|
| template | utf8 | Template string with {} placeholders. |
| Returns | utf8 | The template with placeholders substituted. |
Example
format("{} = {}", "x", 42) // "x = 42"
head
head(xs: [T]) -> TThe first element of a list or tensor (none when empty).
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | A list or tensor. |
| Returns | T | The first element, or none if empty. |
Example
head([10, 20, 30]) // 10
hypot
hypot(a: number, b: number) -> f64Length of the hypotenuse sqrt(a² + b²).
| Parameter | Type | Description |
|---|---|---|
| a | number | The first operand. |
| b | number | The second operand. |
| Returns | f64 | The result, as an f64. |
Example
hypot(3, 4) // 5.0
index_of
index_of(xs: [T], needle: T) -> i64The index of the first element equal to needle, or -1 if absent.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list to search. |
| needle | T | The value to look for. |
| Returns | i64 | The zero-based index, or -1 if not found. |
Example
index_of(["a", "b", "c"], "b") // 1
index_of(["a", "b"], "z") // -1
join
join(parts: [utf8], sep: utf8) -> utf8Join a list of strings into one, inserting a separator between each.
| Parameter | Type | Description |
|---|---|---|
| parts | [utf8] | The strings to join. |
| sep | utf8 | The separator inserted between parts. |
| Returns | utf8 | The joined string. |
Example
join(["a", "b", "c"], "-") // "a-b-c"
len
len(xs: [T]) -> usizeThe number of elements in a list or tensor, or characters in a string.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | A list, tensor, or string. |
| Returns | usize | The number of elements (or characters). |
Example
len([10, 20, 30]) // 3
len("hello") // 5
list_contains
list_contains(xs: [T], needle: T) -> boolWhether a list contains a value equal to needle.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list to search. |
| needle | T | The value to look for. |
| Returns | bool | true if an equal element is present. |
Example
list_contains([1, 2, 3], 2) // true
ln
ln(x: number) -> f64Natural (base-e) logarithm.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
ln(e()) // 1.0
log10
log10(x: number) -> f64Base-10 logarithm.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
log10(1000) // 3.0
log2
log2(x: number) -> f64Base-2 logarithm.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
log2(8) // 3.0
map
map(xs: [T], f: fn (T) -> U) -> [U]Apply a function to every element of a list or tensor, returning the transformed collection.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list or tensor to transform. |
| f | fn (T) -> U | Function applied to each element. |
| Returns | [U] | A new collection of the transformed elements. |
Example
map([1, 2, 3], fn(x: i64) -> i64 x * 2) // [2, 4, 6]
max
max(a: number, b: number) -> f64The larger of two numbers.
| Parameter | Type | Description |
|---|---|---|
| a | number | The first operand. |
| b | number | The second operand. |
| Returns | f64 | The result, as an f64. |
Example
max(3, 7) // 7.0
min
min(a: number, b: number) -> f64The smaller of two numbers.
| Parameter | Type | Description |
|---|---|---|
| a | number | The first operand. |
| b | number | The second operand. |
| Returns | f64 | The result, as an f64. |
Example
min(3, 7) // 3.0
panic
panic(msg: utf8) -> neverAbort evaluation with an unrecoverable failure message.
| Parameter | Type | Description |
|---|---|---|
| msg | utf8 | The failure message to report. |
| Returns | never | Never returns — aborts evaluation. |
Example
panic("unreachable state reached")
pi
pi() -> f64The constant π (≈ 3.14159).
| Parameter | Type | Description |
|---|---|---|
| Returns | f64 | The value of π. |
Example
pi() // 3.14159…
pow
pow(a: number, b: number) -> f64Raise a to the power b.
| Parameter | Type | Description |
|---|---|---|
| a | number | The first operand. |
| b | number | The second operand. |
| Returns | f64 | The result, as an f64. |
Example
pow(2, 10) // 1024.0
radians
radians(x: number) -> f64Convert an angle from degrees to radians.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
radians(180) // 3.14159…
range
range(start: i64, end: i64) -> [i64]The half-open integer range [start, end) as a list.
| Parameter | Type | Description |
|---|---|---|
| start | i64 | Inclusive lower bound. |
| end | i64 | Exclusive upper bound; must be >= start. |
| Returns | [i64] | The integers from start up to (but excluding) end. |
Example
range(1, 4) // [1, 2, 3]
replace
replace(s: utf8, old: utf8, new: utf8) -> utf8Replace every occurrence of a substring with another.
| Parameter | Type | Description |
|---|---|---|
| s | utf8 | The string to search. |
| old | utf8 | The substring to find. |
| new | utf8 | The replacement substring. |
| Returns | utf8 | The string with every match replaced. |
Example
replace("hello world", "world", "there") // "hello there"
reverse
reverse(xs: [T]) -> [T]Reverse the order of a list's elements.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list to reverse. |
| Returns | [T] | The list in reverse order. |
Example
reverse([1, 2, 3]) // [3, 2, 1]
round
round(x: number) -> f64Round to the nearest integer (ties away from zero).
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
round(2.5) // 3.0
sign
sign(x: number) -> f64The sign of x: 1, -1, or 0.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
sign(-3) // -1.0
sin
sin(x: number) -> f64Sine of an angle in radians.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
sin(pi() / 2) // 1.0
sort
sort(xs: [T]) -> [T]Sort a list — numerically for all-numeric lists, lexicographically for all-string lists.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | An all-numeric or all-string list. |
| Returns | [T] | The sorted list. |
Example
sort([3, 1, 2]) // [1, 2, 3]
sort(["b", "a", "c"]) // ["a", "b", "c"]
sort_connected
sort_connected(items: [T], edges: [{source, destination, ...}]) -> [T]Reorder a list so that items joined by edges cluster together (recursing into children).
| Parameter | Type | Description |
|---|---|---|
| items | [T] | Items identified by an id field (possibly nested via children). |
| edges | [{source, destination, ...}] | Edge records linking item ids. |
| Returns | [T] | The reordered list, connected items adjacent. |
Example
// Reorder items so nodes joined by edges sit next to each other.
sort_connected(nodes, edges)
split
split(s: utf8, sep: utf8) -> [utf8]Split a string on every occurrence of a separator into a list of pieces.
| Parameter | Type | Description |
|---|---|---|
| s | utf8 | The string to split. |
| sep | utf8 | The separator to split on. |
| Returns | [utf8] | The pieces between separators. |
Example
split("a,b,c", ",") // ["a", "b", "c"]
sqrt
sqrt(x: number) -> f64Square root.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
sqrt(16) // 4.0
starts_with
starts_with(s: utf8, prefix: utf8) -> boolWhether a string begins with a prefix.
| Parameter | Type | Description |
|---|---|---|
| s | utf8 | The string to test. |
| prefix | utf8 | The prefix to look for. |
| Returns | bool | true if the string starts with the prefix. |
Example
starts_with("hello", "he") // true
sum
sum(xs: [number]) -> numberAdd together every element of a non-empty homogeneous numeric list or tensor.
| Parameter | Type | Description |
|---|---|---|
| xs | [number] | A non-empty list or tensor of one numeric type. |
| Returns | number | The total, in the element's numeric type. |
Example
sum([1.5, 2.5, 3.0]) // 7.0
tail
tail(xs: [T]) -> [T]Every element of a list or tensor except the first.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | A list or tensor. |
| Returns | [T] | The elements after the first. |
Example
tail([10, 20, 30]) // [20, 30]
take
take(xs: [T], n: i64) -> [T]The first n elements of a list (fewer if the list is shorter).
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list to take from. |
| n | i64 | How many leading elements to keep. |
| Returns | [T] | The first n elements. |
Example
take([1, 2, 3, 4], 2) // [1, 2]
tan
tan(x: number) -> f64Tangent of an angle in radians.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
tan(0) // 0.0
tau
tau() -> f64The constant τ = 2π (≈ 6.28319).
| Parameter | Type | Description |
|---|---|---|
| Returns | f64 | The value of τ (2π). |
Example
tau() // 6.28318…
tensor
tensor(data: [number], shape: [usize]) -> tensor<T>Build a tensor from flat row-major data and a shape; the data length must equal the product of the dimensions.
| Parameter | Type | Description |
|---|---|---|
| data | [number] | Flat, row-major element data. |
| shape | [usize] | The dimension sizes. |
| Returns | tensor<T> | The constructed tensor. |
Example
tensor([1, 2, 3, 4, 5, 6], [2, 3]) // a 2×3 tensor
tensor_data
tensor_data(t: tensor<T>) -> [T]The flat row-major element data of a tensor as a list.
| Parameter | Type | Description |
|---|---|---|
| t | tensor<T> | The tensor to read. |
| Returns | [T] | The tensor's flat, row-major element data. |
Example
tensor_data(tensor([1, 2, 3, 4], [2, 2])) // [1, 2, 3, 4]
tensor_reshape
tensor_reshape(t: tensor<T>, shape: [usize]) -> tensor<T>Reinterpret a tensor's data under a new shape; the element count must be unchanged.
| Parameter | Type | Description |
|---|---|---|
| t | tensor<T> | The tensor to reshape. |
| shape | [usize] | The new dimension sizes. |
| Returns | tensor<T> | The same data under the new shape. |
Example
tensor_reshape(tensor([1, 2, 3, 4], [2, 2]), [4]) // shape [4]
tensor_shape
tensor_shape(t: tensor<T>) -> [usize]The dimension sizes of a tensor as a list.
| Parameter | Type | Description |
|---|---|---|
| t | tensor<T> | The tensor to read. |
| Returns | [usize] | The tensor's dimension sizes. |
Example
tensor_shape(tensor([1, 2, 3, 4], [2, 2])) // [2, 2]
to_lower
to_lower(s: utf8) -> utf8Lowercase every character of a string.
| Parameter | Type | Description |
|---|---|---|
| s | utf8 | The string to lowercase. |
| Returns | utf8 | The lowercased string. |
Example
to_lower("AbC") // "abc"
to_upper
to_upper(s: utf8) -> utf8Uppercase every character of a string.
| Parameter | Type | Description |
|---|---|---|
| s | utf8 | The string to uppercase. |
| Returns | utf8 | The uppercased string. |
Example
to_upper("abc") // "ABC"
trim
trim(s: utf8) -> utf8Remove leading and trailing whitespace from a string.
| Parameter | Type | Description |
|---|---|---|
| s | utf8 | The string to trim. |
| Returns | utf8 | The string without leading/trailing whitespace. |
Example
trim(" hi ") // "hi"
trunc
trunc(x: number) -> f64Discard the fractional part, rounding toward zero.
| Parameter | Type | Description |
|---|---|---|
| x | number | The input value (any number, widened to f64). |
| Returns | f64 | The result, as an f64. |
Example
trunc(2.9) // 2.0
type_fields
type_fields(target: &T) -> [record]Reflect a type or interface into a list of field-description records (own fields first, then inherited via extends).
| Parameter | Type | Description |
|---|---|---|
| target | &T | A reference to a type or interface declaration. |
| Returns | [record] | One record per field: { name, type, isfunction, optional, hasdefault, is_block, repeated, accepts, decorators }. |
Example
type_fields(Image) // [{ name: "source", type: "utf8", … }, …]
unique
unique(xs: [T]) -> [T]Remove duplicate elements from a list, keeping first-seen order.
| Parameter | Type | Description |
|---|---|---|
| xs | [T] | The list to deduplicate. |
| Returns | [T] | The list with duplicates removed. |
Example
unique([1, 2, 2, 3, 1]) // [1, 2, 3]
zip
zip(a: [A], b: [B]) -> [(A, B)]Pair up elements of two lists by index, stopping at the shorter length.
| Parameter | Type | Description |
|---|---|---|
| a | [A] | The first list. |
| b | [B] | The second list. |
| Returns | [(A, B)] | Index-paired [a, b] lists, up to the shorter length. |
Example
zip([1, 2, 3], ["a", "b", "c"]) // [[1, "a"], [2, "b"], [3, "c"]]