Built-in functions

Everything available without an import: I/O, type checking, conversion, math, collections, functional helpers, and debugging.

Summary

Built-ins are always in scope. I/O: println, print, eprintln, input. Type: type(), typeof(), and the is_* predicates. Conversion: int(), float(),str(), bool(), chr(), ord(). Math:abs, min, max, sqrt, floor,ceil, round. Collections: len, range,enumerate, zip, flatten, sorted,sum. Functional: map, filter, reduce. Debugging: assert, assert_eq, dbg, repr,pprint, panic, todo, unreachable. Constants: PI, E, INF, NAN.

Canonical

I/O

FunctionDescription
println(args...)Print with newline. Supports {} placeholders
print(args...)Print without trailing newline
eprint(args...)Print to stderr without newline
eprintln(args...)Print to stderr with newline
input(prompt?)Read line from stdin
clear()Clear terminal screen
println("x = {}", 42)           -- x = 42  (positional {} placeholder)
println("hi", "there")          -- hi there (space-separated if no {})

Type Checking

FunctionDescription
type(val)Type name lowercase: "int", "str", "array", etc.
typeof(val)Alias for type()
type_of(val)Type name capitalized: "Int", "Str", "Array", etc.
is_null(val)Check if null
is_int(val)Check if integer
is_float(val)Check if float
is_str(val)Check if string
is_bool(val)Check if boolean
is_array(val)Check if array
is_fn(val)Check if function

Conversion

FunctionDescription
int(val) / i64(val)Convert to integer (truncates floats toward zero)
float(val) / f64(val)Convert to float
str(val)Convert to string
bool(val)Convert to boolean
char(n) / chr(n)Integer to single-character string
ord(ch)Character (first byte) to integer
println(int(3.9))                -- 3  (toward zero)
println(int(-3.9))               -- -3
println(chr(65))                 -- A
println(ord("A"))                -- 65
println(str(42))                 -- "42"
println(float(10))               -- 10.0

Math

FunctionDescription
abs(x)Absolute value
min(a, b)Minimum of two values
max(a, b)Maximum of two values
pow(base, exp)Power
sqrt(x)Square root
floor(x)Floor
ceil(x)Ceiling
round(x)Round to nearest
log(x)Natural logarithm
sin(x) cos(x) tan(x)Trig functions (radians)

Collections

FunctionDescription
len(val)Length of array, string, map, tuple, or range
range(n)Create range 0..n
array()Empty array
map()Empty map
sorted(arr)Sorted copy of array
sum(arr)Sum of numeric array
enumerate(arr)Array of (index, value) tuples
zip(a, b)Zip two arrays into tuples
flatten(arr)Flatten one level
keys(map)Map keys as array
values(map)Map values as array
entries(map)Map entries as (key, value) tuples
chars(str)String to array of characters
bytes(str)String to array of byte values
contains(str, sub)Check if string contains substring
println(enumerate([10, 20, 30]))  -- [(0, 10), (1, 20), (2, 30)]
println(zip([1, 2], [3, 4]))     -- [(1, 3), (2, 4)]
println(range(5))                -- 0..5

Functional

FunctionDescription
map(arr, fn)Apply fn to each element
filter(arr, fn)Keep elements where fn returns truthy
reduce(arr, fn, init)Reduce array to single value
println(map([1, 2, 3], fn(x) { x * 2 }))      -- [2, 4, 6]
println(filter([1, 2, 3, 4], fn(x) { x > 2 })) -- [3, 4]
println(reduce([1, 2, 3], fn(a, b) { a + b }, 0))  -- 6

Debugging and Testing

FunctionDescription
assert(cond, msg?)Assert truthy; panics on failure
assert_eq(a, b)Assert a == b; shows both values on failure
dbg(val)Print [dbg] <repr> to stderr, returns val
pprint(val)Pretty-print with indentation
repr(val)Debug string representation
panic(msg)Print to stderr and exit (not catchable)
todo(msg?)Mark unimplemented; panics
unreachable()Mark unreachable; panics if reached
exit(code)Exit with given code

Copying

FunctionDescription
copy(val) / clone(val)Shallow copy of a value

Result / Option Constructors

println(Ok(42))                  -- Ok(42)
println(Err("bad"))              -- Err(bad)
println(Some(10))                -- Some(10)
println(None())                  -- null

These are used for explicit result / option values you can match on.

String Formatting

-- positional placeholders
println(format("hello {} you are {}", "world", 42))
-- hello world you are 42

sprintf is an alias for format.

Constants

NameValue
PI3.14159265358979...
E2.71828182845904...
INFInfinity (float)
NANNot a Number (float)

Command-line Arguments

Read positional args via os.args. The list excludes the interpreter path and the script filename:

import os
-- Run: xs script.xs hello world
println(os.args)                 -- ["hello", "world"]