Functions and lists

Give a function typed inputs and a return type, then call it by name.

fn double(value: Number): Number { return value * 2 }
double(21)

Call functions with XML

An XML tag calls a function with named arguments. The function can return any type.

fn badge(numeric: Number, mode: "short" | "long", label: String, active: Bool): String {
    return "{label} {numeric} {mode} {active}"
}

<badge numeric-1 mode-short label="Take" active />
  • A bare active supplies true. Omitting a Bool argument supplies false in XML. Use active={expression} for a computed value.
  • A numeric argument can use numeric-1, numeric--1 for negative values, or a unit such as width-24pt.
  • A string-literal choice can use mode-short. For an unrestricted String, write label="Take".
  • Style utilities use the same parameter names. For example, w-24 can supply a user-defined function's width argument in points.

Put nested content between the tags to pass it as the children list. Plain text in an element list becomes a Text element. Use braces such as {"hello"} to pass a string value instead. Generic functions infer their type arguments from the arguments and children. A tag can also supply a type explicitly.

fn collect<T>(children: List<T>): List<T> { return children }
fn identity<T>(value: T): T { return value }

<collect>
    <identity<Number> value-3 />
    {4}
</collect>

Pure functions and capabilities

Functions are pure by default: they read their arguments, immutable outer let values, and pure helpers. Local scratch mutation is allowed. Mutable parameters, live signals, external reads, and observable effects require explicit capabilities:

let offset = 2
fn double(value: Number): Number { return value * 2 + offset }

mut count = 0
fn<ref> current_count(): Number { return count }
fn<mut, ref> increment(): Unit { count = count + 1 }
fn introduction(): Animation { return wait(2s) }
  • mut permits mutable parameters and observable writes.
  • sig permits live signal parameters and returns.
  • ref permits reading outer mutable/signal state or external resources.

Capabilities belong to function types (e.g., fn (value: Number): Number or fn<ref> (): Number). A caller must permit the capabilities its callees require. Regular boolean assert(condition, message=...) and read-only data queries are pure.

Random values

Random(seed=42) gives you a reproducible source of random values. The seed defaults to 0. Seeds and sample indices are nonnegative 64-bit integers. Each method uses the index you give it, so you can sample in any order and still get the same results.

let rng = Random(seed = 42)
let x = rng.number(0, min = -40%, max = 40%)
let count = rng.integer(1, min = 1, max = 5)
let visible = rng.chance(2, probability = 0.75)
let direction = rng.choose(3, [-1, 1])
{x=x, count=count, visible=visible, direction=direction}

number and integer include min and exclude max. With no bounds, number samples from 0 up to 1. Pass two bounds with the same unit to sample a percentage, duration, angle, or other unit directly. integer requires both bounds. chance accepts a probability from 0 to 1, and choose returns one element from a nonempty list or array.

Function documentation

Put consecutive # or ## comment lines immediately above a function declaration to document it. The language server shows the Markdown summary in hover, completion, and signature help. Use @param for an individual parameter, @returns for the result, and @example for example code.

## Offset a position by the scene margin.
## @param position - The original horizontal position.
## @returns The position with the margin added.
fn inset(position: Length): Length { return position + 8pt }

Write separate comments above overload clauses to describe their behavior. The language server also displays documentation for standard library functions, types, constants, and methods.

Anonymous functions

Anonymous functions use the same typed parameter and return syntax and can capture lexical bindings:

let double = fn (value: Number): Number { return value * 2 }
let answers = [1, 2, 3].map(fn (value: Number): Number {
    return double(value)
})
answers

Partial application

Supply one or more named arguments to a single-clause function and leave a required argument open to get a function for the remaining parameters. Ebi checks the supplied arguments against their declared types. If all required arguments are present, Ebi calls the function and uses defaults for any omitted optional arguments.

fn shift(value: Number, offset: Number): Number {
    return value + offset
}

let plus_two = shift(offset = 2)
let operations = { shift = plus_two }
let values = [1, 2, 3].map(operations.shift)
values

A partial call samples ordinary arguments when it binds them. A sig parameter keeps its input live. You can pass the resulting function to another function, return it, or put it in an immutable list or record.

Signal parameters and returns

See Signals for live parameters and return values.

Guarded function clauses

A function can have multiple disjoint clauses selected by literal strings or numeric refinements:

fn delay(time: "1s"): Duration { return 1s }
fn delay(time: "2s"): Duration { return 2s }

fn repetitions(count: Int < 1): Duration { return 0s }
fn repetitions(count: Int >= 1): Duration { return count * 1s }

delay("1s") + repetitions(3)

Int is an integer refinement of Number. Comparisons use matching units (e.g., Duration <= 1s). Clause domains must be provably disjoint—overlap is an error rather than source-order priority.

List lengths in types

List<T, N> describes exactly N elements. Omitting N or writing _ leaves the length unspecified:

let values = [1, 2, 3]                 # List<Number, 3>
let triple: List<Number, 3> = values
let arbitrary: List<Number> = values
let also_arbitrary: List<Number, _> = values
type Pair<T> = List<T, 2>

The checker enforces known lengths in assignments, calls, returns, and typed reads. map, scan, sort, and reverse preserve length; zip takes the shorter length; concat adds lengths.

Typed persistent-list operations

List methods return new immutable lists and accept named, generic, guarded, or anonymous callbacks:

fn square(value: Number): Number { return value * value }
fn positive(value: Number): Bool { return value > 0 }
fn combine(total: Number, value: Number): Number { return total + value }

let values = [-2, 1, 3, 3]
let squares = values.map(square)
let positives = values.filter(positive)
let total = values.fold(0, combine)
let running = values.scan(0, combine)
let distinct = values.sort().unique()
let insertion = distinct.bisect_right(3)
{squares=squares, positives=positives, total=total, running=running,
 distinct=distinct, insertion=insertion}
MethodBehavior
map(callback) / flat_map(callback)Transform each item; flat-map concatenates returned lists.
filter(callback)Keep items whose predicate returns true.
reduce(callback)Combine from the first item; an empty list is an error.
fold(initial, callback)Combine from an explicit initial value; empty input returns it.
scan(initial, callback)Return the accumulator after each item; output length equals input length.
any / all / find / positionShort-circuit a predicate. find and position return Option.
enumerate() / zip(other)Return records {index, value} or {left, right}; zip stops at the shorter input.
take(count) / drop(count) / slice(start=..., end=..., step=...)Select sublists. Slicing uses the same bounds as items[start:end:step].
reverse()Return reversed values without mutating the input.
sort(key=..., reverse=false)Stable sort; optional scalar keys are evaluated once per item.
unique(key=...)Keep each scalar key's first occurrence in input order.
bisect_left(value, lo=..., hi=...)Insertion position before equal keys in ascending sorted input.
bisect_right(value, lo=..., hi=...)Insertion position after equal keys in ascending sorted input.