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

Functions

abs

abs(x: number) -> f64

Absolute value.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

abs(-5)   // 5.0

acos

acos(x: number) -> f64

Arccosine, in radians, of a value in [-1, 1].

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

acos(1)   // 0.0

asin

asin(x: number) -> f64

Arcsine, in radians, of a value in [-1, 1].

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

asin(1)   // 1.5707963…  (π/2)

assert

assert(cond: bool, msg: utf8) -> none

Return none when cond is true, otherwise abort with msg.

ParameterTypeDescription
condboolThe condition that must hold.
msgutf8The error message reported when cond is false.
Returnsnonenone when the assertion holds (otherwise aborts).

Example

assert(len(xs) > 0, "list must not be empty")

ast_string

ast_string(target: &T) -> utf8

Pretty-print the canonical source behind a reference (type/interface/union/symbol_set/block/field) or a function value.

ParameterTypeDescription
target&TA dataref to a declaration, or a function value.
Returnsutf8The canonical (pretty-printed) source text.

Example

ast_string(Image)   // the Image type's source, pretty-printed

at

at(xs: [T], i: i64) -> T

The element at a zero-based index; errors if out of bounds or negative.

ParameterTypeDescription
xs[T]The list to index.
ii64The zero-based index.
ReturnsTThe element at i.

Example

at([10, 20, 30], 1)   // 20

atan

atan(x: number) -> f64

Arctangent, in radians.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

atan(1)   // 0.7853981…  (π/4)

atan2

atan2(a: number, b: number) -> f64

Arctangent of a/b in radians, using the signs of both to pick the quadrant.

ParameterTypeDescription
anumberThe first operand.
bnumberThe second operand.
Returnsf64The 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.

ParameterTypeDescription
Returns[utf8]Every built-in's name, sorted alphabetically.

Example

builtin_names()   // ["abs", "acos", …, "zip"]

cbrt

cbrt(x: number) -> f64

Cube root.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

cbrt(27)   // 3.0

ceil

ceil(x: number) -> f64

Round up to the nearest integer.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The 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.

ParameterTypeDescription
target&TA 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) -> f64

Constrain x to the range [lo, hi].

ParameterTypeDescription
xnumberThe value to clamp.
lonumberThe lower bound.
hinumberThe upper bound.
Returnsf64x limited to [lo, hi], as an f64.

Example

clamp(12, 0, 10)   // 10.0

concat

concat(a: utf8, b: utf8) -> utf8

Concatenate two strings into one.

ParameterTypeDescription
autf8The left-hand string.
butf8The string appended after a.
Returnsutf8The two strings joined together.

Example

concat("foo", "bar")   // "foobar"

contains

contains(s: utf8, needle: utf8) -> bool

Whether a string contains a substring.

ParameterTypeDescription
sutf8The string to search.
needleutf8The substring to look for.
Returnsbooltrue if the substring is present.

Example

contains("hello", "ell")   // true

cos

cos(x: number) -> f64

Cosine of an angle in radians.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

cos(0)   // 1.0

decorator_arg

decorator_arg(target: &T, decorator: utf8, slot: utf8) -> any

Read one named argument of a decorator on a referenced declaration (none if absent).

ParameterTypeDescription
target&TA reference to a type, field, block, or variant.
decoratorutf8The decorator name, e.g. "doc".
slotutf8The argument (slot) name to read.
ReturnsanyThe 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.

ParameterTypeDescription
target&TA 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) -> f64

Convert an angle from radians to degrees.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The 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.

ParameterTypeDescription
xs[T]The list to drop from.
ni64How many leading elements to skip.
Returns[T]The elements after the first n.

Example

drop([1, 2, 3, 4], 2)   // [3, 4]

e

e() -> f64

Euler's number e (≈ 2.71828).

ParameterTypeDescription
Returnsf64The value of e.

Example

e()   // 2.71828…

ends_with

ends_with(s: utf8, suffix: utf8) -> bool

Whether a string ends with a suffix.

ParameterTypeDescription
sutf8The string to test.
suffixutf8The suffix to look for.
Returnsbooltrue if the string ends with the suffix.

Example

ends_with("hello", "lo")   // true

error

error(msg: utf8) -> never

Abort evaluation with an error message.

ParameterTypeDescription
msgutf8The error message to report.
ReturnsneverNever returns — aborts evaluation.

Example

if found { value } else { error("config not found") }

eval

eval(src: utf8) -> any

Parse a string as a WCL expression and evaluate it in the current scope.

ParameterTypeDescription
srcutf8WCL expression source to parse and evaluate.
ReturnsanyThe value the expression evaluates to.

Example

eval("1 + 2 * 3")   // 7

exp

exp(x: number) -> f64

e raised to the power x.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The 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.

ParameterTypeDescription
xs[T]The list to filter.
predfn (T) -> boolPredicate 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.

ParameterTypeDescription
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) -> f64

Round down to the nearest integer.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

floor(2.7)   // 2.0

fn_signature

fn_signature(f: any) -> record

Describe a function's parameters and return type. Pass a function value, or a built-in's name as a string.

ParameterTypeDescription
fanyA function value, or the name of a built-in as a utf8 string.
ReturnsrecordA 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) -> U

Reduce a list or tensor to a single value by repeatedly combining the accumulator with each element.

ParameterTypeDescription
xs[T]The list or tensor to reduce.
initUThe initial accumulator value.
ffn (U, T) -> UCombines the accumulator with the next element.
ReturnsUThe final accumulator value.

Example

fold([1, 2, 3, 4], 0, fn(acc: i64, x: i64) -> i64 acc + x)   // 10

format

format(utf8, ...args) -> utf8

Substitute trailing arguments into a template's {} placeholders ({{/}} are literal braces).

ParameterTypeDescription
templateutf8Template string with {} placeholders.
Returnsutf8The template with placeholders substituted.

Example

format("{} = {}", "x", 42)   // "x = 42"
head(xs: [T]) -> T

The first element of a list or tensor (none when empty).

ParameterTypeDescription
xs[T]A list or tensor.
ReturnsTThe first element, or none if empty.

Example

head([10, 20, 30])   // 10

hypot

hypot(a: number, b: number) -> f64

Length of the hypotenuse sqrt(a² + b²).

ParameterTypeDescription
anumberThe first operand.
bnumberThe second operand.
Returnsf64The result, as an f64.

Example

hypot(3, 4)   // 5.0

index_of

index_of(xs: [T], needle: T) -> i64

The index of the first element equal to needle, or -1 if absent.

ParameterTypeDescription
xs[T]The list to search.
needleTThe value to look for.
Returnsi64The 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) -> utf8

Join a list of strings into one, inserting a separator between each.

ParameterTypeDescription
parts[utf8]The strings to join.
seputf8The separator inserted between parts.
Returnsutf8The joined string.

Example

join(["a", "b", "c"], "-")   // "a-b-c"

len

len(xs: [T]) -> usize

The number of elements in a list or tensor, or characters in a string.

ParameterTypeDescription
xs[T]A list, tensor, or string.
ReturnsusizeThe number of elements (or characters).

Example

len([10, 20, 30])   // 3
len("hello")        // 5

list_contains

list_contains(xs: [T], needle: T) -> bool

Whether a list contains a value equal to needle.

ParameterTypeDescription
xs[T]The list to search.
needleTThe value to look for.
Returnsbooltrue if an equal element is present.

Example

list_contains([1, 2, 3], 2)   // true

ln

ln(x: number) -> f64

Natural (base-e) logarithm.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

ln(e())   // 1.0

log10

log10(x: number) -> f64

Base-10 logarithm.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

log10(1000)   // 3.0

log2

log2(x: number) -> f64

Base-2 logarithm.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The 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.

ParameterTypeDescription
xs[T]The list or tensor to transform.
ffn (T) -> UFunction 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) -> f64

The larger of two numbers.

ParameterTypeDescription
anumberThe first operand.
bnumberThe second operand.
Returnsf64The result, as an f64.

Example

max(3, 7)   // 7.0

min

min(a: number, b: number) -> f64

The smaller of two numbers.

ParameterTypeDescription
anumberThe first operand.
bnumberThe second operand.
Returnsf64The result, as an f64.

Example

min(3, 7)   // 3.0

panic

panic(msg: utf8) -> never

Abort evaluation with an unrecoverable failure message.

ParameterTypeDescription
msgutf8The failure message to report.
ReturnsneverNever returns — aborts evaluation.

Example

panic("unreachable state reached")

pi

pi() -> f64

The constant π (≈ 3.14159).

ParameterTypeDescription
Returnsf64The value of π.

Example

pi()   // 3.14159…

pow

pow(a: number, b: number) -> f64

Raise a to the power b.

ParameterTypeDescription
anumberThe first operand.
bnumberThe second operand.
Returnsf64The result, as an f64.

Example

pow(2, 10)   // 1024.0

radians

radians(x: number) -> f64

Convert an angle from degrees to radians.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The 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.

ParameterTypeDescription
starti64Inclusive lower bound.
endi64Exclusive 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) -> utf8

Replace every occurrence of a substring with another.

ParameterTypeDescription
sutf8The string to search.
oldutf8The substring to find.
newutf8The replacement substring.
Returnsutf8The 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.

ParameterTypeDescription
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) -> f64

Round to the nearest integer (ties away from zero).

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

round(2.5)   // 3.0

sign

sign(x: number) -> f64

The sign of x: 1, -1, or 0.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

sign(-3)   // -1.0

sin

sin(x: number) -> f64

Sine of an angle in radians.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The 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.

ParameterTypeDescription
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).

ParameterTypeDescription
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.

ParameterTypeDescription
sutf8The string to split.
seputf8The separator to split on.
Returns[utf8]The pieces between separators.

Example

split("a,b,c", ",")   // ["a", "b", "c"]

sqrt

sqrt(x: number) -> f64

Square root.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

sqrt(16)   // 4.0

starts_with

starts_with(s: utf8, prefix: utf8) -> bool

Whether a string begins with a prefix.

ParameterTypeDescription
sutf8The string to test.
prefixutf8The prefix to look for.
Returnsbooltrue if the string starts with the prefix.

Example

starts_with("hello", "he")   // true

sum

sum(xs: [number]) -> number

Add together every element of a non-empty homogeneous numeric list or tensor.

ParameterTypeDescription
xs[number]A non-empty list or tensor of one numeric type.
ReturnsnumberThe 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.

ParameterTypeDescription
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).

ParameterTypeDescription
xs[T]The list to take from.
ni64How many leading elements to keep.
Returns[T]The first n elements.

Example

take([1, 2, 3, 4], 2)   // [1, 2]

tan

tan(x: number) -> f64

Tangent of an angle in radians.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The result, as an f64.

Example

tan(0)   // 0.0

tau

tau() -> f64

The constant τ = 2π (≈ 6.28319).

ParameterTypeDescription
Returnsf64The 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.

ParameterTypeDescription
data[number]Flat, row-major element data.
shape[usize]The dimension sizes.
Returnstensor<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.

ParameterTypeDescription
ttensor<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.

ParameterTypeDescription
ttensor<T>The tensor to reshape.
shape[usize]The new dimension sizes.
Returnstensor<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.

ParameterTypeDescription
ttensor<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) -> utf8

Lowercase every character of a string.

ParameterTypeDescription
sutf8The string to lowercase.
Returnsutf8The lowercased string.

Example

to_lower("AbC")   // "abc"

to_upper

to_upper(s: utf8) -> utf8

Uppercase every character of a string.

ParameterTypeDescription
sutf8The string to uppercase.
Returnsutf8The uppercased string.

Example

to_upper("abc")   // "ABC"

trim

trim(s: utf8) -> utf8

Remove leading and trailing whitespace from a string.

ParameterTypeDescription
sutf8The string to trim.
Returnsutf8The string without leading/trailing whitespace.

Example

trim("  hi  ")   // "hi"

trunc

trunc(x: number) -> f64

Discard the fractional part, rounding toward zero.

ParameterTypeDescription
xnumberThe input value (any number, widened to f64).
Returnsf64The 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).

ParameterTypeDescription
target&TA 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.

ParameterTypeDescription
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.

ParameterTypeDescription
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"]]