Trainerflow Docs trainerflow.com
Standard library › Built-in functions

Standard library

Built-in functions

The handful of functions every program can call without importing anything.

Built-in functions are part of Coach itself, so there is nothing to import. They can only be called: you cannot store one in a variable or pass it to an exec. A let, an exec, a group or a parameter with the same name hides the built-in, and the editor warns you when that happens.

The numeric functions accept measurements as well as numbers, as long as the units measure the same thing, and they answer in the unit of their first argument. All of them also work in the value expressions of insights.

Checking a value

is_number(x)

Whether the value is a plain number.

is_number(12)      // true
is_number(12|kg|)  // false
is_number("12")    // false

is_string(x)

Whether the value is a string.

is_string("slow")  // true
is_string(3)       // false

is_boolean(x)

Whether the value is true or false.

is_boolean(false)  // true
is_boolean(0)      // false

is_measurement(x)

Whether the value is a measurement with a unit, like 80|kg|.

is_measurement(80|kg|)  // true
is_measurement(80)      // false

is_range(x)

Whether the value is a range, like 8..12.

is_range(8..12)  // true
is_range(8)      // false

is_collection(x)

Whether the value is a list or a map.

is_collection([1, 2])  // true
is_collection([a: 1])  // true
is_collection("ab")    // false

Rounding

round(x) or round(x, step)

Round to the nearest whole number, or to the nearest multiple of a step. Halves round up.

round(2.5)              // 3
round(99|kg|, 2.5|kg|)  // 100|kg|
round(7, 5)             // 5
round(0.31, 0.1)        // 0.3

floor(x) or floor(x, step)

Round down to a whole number, or to a multiple of a step.

floor(2.9)               // 2
floor(104|kg|, 2.5|kg|)  // 102.5|kg|

ceil(x) or ceil(x, step)

Round up to a whole number, or to a multiple of a step.

ceil(2.1)               // 3
ceil(101|kg|, 2.5|kg|)  // 102.5|kg|

Numbers

abs(x)

The value without its sign.

abs(-4)      // 4
abs(-2|kg|)  // 2|kg|

min(a, b, ...)

The smallest of two or more numbers, or of measurements in one kind of unit, in the unit of the first.

min(3, 1, 2)        // 1
min(1|kg|, 500|g|)  // 0.5|kg|

max(a, b, ...)

The largest of two or more numbers, or of measurements in one kind of unit, in the unit of the first.

max(3, 1, 2)         // 3
max(1|kg|, 1500|g|)  // 1.5|kg|

Collections and strings

len(x)

How many items a list or map holds, or how many characters a string has.

len([4, 5, 6])  // 3
len("tempo")    // 5

has(collection, key)

Whether a map has a key, or a list has a position.

has([down: 3], "down")  // true
has([down: 3], "up")    // false
has([4, 5], 1)          // true