Standard library
The built-in functions, always in scope.
Every function below is a global, and is also grouped under a
frozen namespace with a short, underscore-free name — use whichever you
prefer, they're the same function:
io | print, input, error, assert |
math | sqrt, pow, floor, round, min, max, clamp, random, PI, … — math.sqrt(x) |
number | int, float, num, parse (was parse_int), hex, bin, oct — number.parse("ff", 16) |
string | upper, lower, trim, split, join, replace, starts/ends (were starts_with/ends_with) — string.starts(s, p) |
list | push, pop, insert, remove (was remove_at), sort, map, filter, reduce, … — list.remove(xs, i) |
dict | keys, values, items, has, get, delete — dict.keys(m) |
is | number, string, list, map, nil, bool, function — is.number(x) (was is_number) |
The flat names (sqrt, is_number, …) remain as
aliases, so existing code keeps working.
I/O
print(...) | Write arguments (space-separated) and a newline. |
input([prompt]) | Read a line from stdin; returns a string or nil at EOF. |
Conversion
str(v) | Value to display string. |
repr(v) | Value to a quoted/debug string. |
num(v), int(v), float(v) | Parse/convert to a number. |
bool(v) | Truthiness as a bool. |
hex(n), bin(n), oct(n) | Integer to a 0x/0b/0o string. |
parse_int(s[, base]) | String to integer in the given base (inverse of the above). |
chars(s) | String to a list of one-character strings. |
Type checks
type(v) | Type name as a string. |
is_number, is_string, is_list, is_map, is_nil, is_bool, is_function | Each takes one value, returns a bool. |
Lists
len(x) | Length of a list, string, or map. |
push(list, ...), pop(list) | Append / remove-last (mutate in place). |
insert(list, i, v), remove_at(list, i) | Insert at / remove at an index (mutate). |
range(stop | start, stop[, step]) | Build a list of numbers. |
slice(seq[, start[, stop]]) | Sub-list or substring. |
sort(list[, cmp]) | New sorted list; cmp(a,b) truthy when a sorts first. |
reverse(seq) | New reversed list or string. |
find(seq, x), contains(seq, x) | Index of first match (or -1) / membership. |
sum(list), count(list, x) | Sum of numbers / count of matches. |
zip(a, b), enumerate(list) | Pair up two lists / pair each item with its index. |
get(seq, i[, default]) | Element or a default if out of range. |
Maps
keys(m), values(m), items(m) | Keys / values / [key, value] pairs. |
has(m, key), get(m, key[, default]) | Membership / lookup with default. |
delete(m, key) | Remove a key; returns whether it existed. |
Higher-order
map(fn, list) | Apply fn to each item. |
filter(fn, list) | Keep items where fn is truthy. |
reduce(fn, list[, init]) | Fold a list to a single value. |
Strings
upper(s), lower(s), capitalize(s) | Case transforms. |
trim(s) | Strip surrounding whitespace. |
split(s[, sep]), join(list[, sep]) | Split into / join from a list. |
replace(s, old, new) | Replace all occurrences. |
repeat(s, n) | Repeat a string (or list) n times. |
starts_with(s, pre), ends_with(s, suf) | Prefix / suffix tests. |
char(n), ord(s) | Code point → character and back. |
Math
floor, ceil, round(x[, n]), abs, sign | Rounding and sign. |
sqrt, pow(a, b), exp, log(x[, base]) | Powers and logs. |
sin, cos, tan | Trigonometry (radians). |
min(...), max(...), clamp(x, lo, hi) | Extremes and clamping. |
random(), randint(a, b) | Random float in [0,1) / random integer in [a,b]. |
PI, E | Constants. |
General & control
copy(v) | Shallow copy of a list or map. |
clock() | Monotonic time in seconds (for timing). |
assert(cond[, msg]) | Raise an error if cond is falsy. |
error([value]) | Raise a value, catchable by try/catch. |