Core functions
Functions not tied to one type. Any of them can be called as a method: len(x) is the same as x.len.
Output
| Function | What it does |
|---|---|
print(*args) | writes the str of each argument, no separators, no newline |
println(*args) | the same, with \n after each; an array prints one element per line |
eprint(*args) / eprintln(*args) | the same to stderr, for warnings and progress |
debug(*args) | writes the inspect of each argument, returns the first |
mzs
print("a", "b")
println()
println([1, 2])
debug("x", nil)text
ab
1
2
"x"
nilInput
input(prompt = nil) prints the prompt and reads one line without the \n. End of input is nil, so a default goes after ??.
mzs
name = input("Name: ") ?? "anonymous"
age = (input("Age: ") ?? "").trim.int
total = 0
while (line = input()) { total += line.int }Size and type
| Function | What it does |
|---|---|
len(x) | length of a string (in characters), array, dict, range; nil → 0 |
empty(x) | len(x) == 0 |
type(x) | the type name, or the record name (record) |
is(x, name) | type check; an unknown name is an error |
mzs
len("привет") # → 6
nil.len # → 0
[].empty # → true
[type(1), type(1.5), type("a"), type(nil)] # → ["int","float","string","nil"]
[type([]), type({}), type(1..2), type(/a/)] # → ["array","dict","range","regex"]
1.is("int") # → true
(1..3).is("array") # → trueConversions
| Function | What it does |
|---|---|
str(x) | a string; nil → "", array and dict → JSON |
int(x) | the leading digits of a string, a fraction truncated; never fails |
float(x) | the leading number; never fails |
parse_int(s, base = 10) | the whole string as an integer, or nil |
parse_float(s) | the whole string as a float, or nil |
bool(x) | only nil and false are false |
array(x) | range → array, dict → pairs, nil → [], anything else → [x] |
dict(x) | from an array of [k, v] pairs |
json(x) | compact JSON, keys in insertion order |
inspect(x) | like str, but strings quoted and nil → nil |
mzs
str(2.0) # → "2.0"
str(nil) # → ""
[1, "a"].str # → "[1,\"a\"]"
"12abc".int # → 12
"abc".int # → 0
nil.int # → 0
int(-1.9) # → -1
"3.7kg".float # → 3.7
"12abc".parse_int # → nil
" 42 ".parse_int # → 42
"ff".parse_int(16) # → 255
"2.5e3".parse_float # → 2500.0
bool("") # → true
bool(0) # → true
(1..3).array # → [1,2,3]
{a: 1}.array # → [["a",1]]
5.array # → [5]
[["a", 1], ["b", 2]].dict # → {"a":1,"b":2}
{a: [1, nil]}.json # → "{\"a\":[1,null]}"
inspect("yes") # → "\"yes\""int is for arithmetic on host values: $price.int + 100 works even when $price is not set. To check whether it is a number, use parse_int:
mzs
port = "8080x".parse_int ?? 80
port # → 80Copies and chains
| Function | What it does |
|---|---|
dup(x) | a shallow copy of an array or dict |
hash(x) | a hash stable across runs |
tap(x) { … } | calls the closure, returns x |
pipe(x) { … } | calls the closure, returns its result |
mzs
xs = [1]
ys = xs.dup
ys.push(2)
xs # → [1]
" 42 ".pipe { it.trim.int } # → 42
5.tap { println("saw ${it}") } # → 5text
saw 5Constructors
| Function | What it does |
|---|---|
regex(pattern, flags = "") | a regex from a string at run time |
range(a, b = nil, step = 1) | an array of numbers, upper bound excluded |
mzs
"Hello" ~ regex("hel" + "lo", "i") # → true
range(3) # → [0,1,2]
range(1, 7, 2) # → [1,3,5]
range(5, 1, -1) # → [5,4,3,2]Aggregates and rounding
| Function | What it does |
|---|---|
sum(xs) | the sum; empty → 0 |
min(…) / max(…) | an array or several arguments; empty → nil |
abs(x) | absolute value |
round(x, digits = 0) | half away from zero; without digits, an integer |
ceil(x, digits = 0) / floor(x, digits = 0) | up / down |
sort(xs) [{ (a, b) -> … }] | a new sorted array; the closure is a comparator |
mzs
sum(1..4) # → 10
max(1, 7, 3) # → 7
min([]) # → nil
abs(-2) # → 2
round(2.5) # → 3
round(-2.5) # → -3
round(1.256, 2) # → 1.26
floor(1.239, 2) # → 1.23
sort([3, 1, 2]) # → [1,2,3]
[3, 1, 2].sort { (a, b) -> b <=> a } # → [3,2,1]format
format(fmt, *args) works like printf: %s %d %f %x %b %j %%, width, precision, flags - + 0. The % operator does the same.
mzs
format("%.2f", 1.5) # → "1.50"
format("%05d|%s", 42, "x") # → "00042|x"
format("%-6s|", "ab") # → "ab |"
format("%x %b", 255, 5) # → "ff 101"
format("%j", {a: 1}) # → "{\"a\":1}"
format("%{n} pcs", {n: 3}) # → "3 pcs"
"%s-%d" % ["a", 1] # → "a-1"Errors and exit
| Function | What it does |
|---|---|
raise(msg, kind = "raise") | raises an error; a dict sets message, kind, data |
assert(cond, msg = "assertion failed") | raises if cond is false |
defined(name) | whether a name or $var is bound; the operand is not evaluated |
exit(code = 0) | ends the program with code 0–255; try does not catch it |
mzs
try raise("bad") else "caught" # → "caught"
try raise("no money", "billing") else (e) -> e["kind"] # → "billing"
try raise({code: 42}) else (e) -> e["data"]["code"] # → 42
try assert(1 > 2, "wrong") else (e) -> e["message"] # → "wrong"
defined(zzz) # → false
defined($price) # → falseassert(0) does not fail: 0 is true.
Waiting
sleep(seconds) pauses; fractional seconds are fine. The time comes out of the run's budget (-t), and while the script sleeps, async tasks run.
mzs
sleep(0.05)
"awake" # → "awake"If the host allows it
Without permission these names do not exist at all. In the CLI: the --rand and --time flags.
| Function | What it does |
|---|---|
rand(n = 0) | no argument: a float in [0, 1), otherwise an integer 0…n-1 |
uuid() | a random UUID |
now() | the current time |
mzs
rand(6) < 6 # → true
uuid().len # → 36
type(now()) # → "time"