Standard Library
Zolo includes a comprehensive standard library built on top of the Lua runtime. All functions are available automatically without imports.
String Functions #
All string functions are in the string namespace:
| Function | Description | Example |
|---|---|---|
string.trim(s) |
Remove leading/trailing whitespace | string.trim(" hi ") → "hi" |
string.trim_start(s) |
Remove leading whitespace | string.trim_start(" hi") → "hi" |
string.trim_end(s) |
Remove trailing whitespace | string.trim_end("hi ") → "hi" |
string.starts_with(s, prefix) |
Check if starts with prefix | string.starts_with("hello", "he") → true |
string.ends_with(s, suffix) |
Check if ends with suffix | string.ends_with("hello", "lo") → true |
string.contains(s, substr) |
Check if contains substring | string.contains("hello", "ell") → true |
string.split(s, sep) |
Split into array | string.split("a,b,c", ",") → ["a","b","c"] |
string.replace(s, old, new) |
Replace occurrences | string.replace("aa", "a", "b") → "bb" |
string.chars(s) |
Split into characters | string.chars("hi") → ["h","i"] |
string.pad_start(s, len, fill) |
Pad at start | string.pad_start("42", 5, "0") → "00042" |
string.pad_end(s, len, fill) |
Pad at end | string.pad_end("hi", 5, ".") → "hi..." |
string.is_empty(s) |
Check if empty | string.is_empty("") → true |
Array Functions #
All array functions are in the Array namespace:
Creation and Basic Operations #
| Function | Description |
|---|---|
Array.new(...) |
Create array from arguments |
Array.len(arr) |
Get array length |
Array.push(arr, val) |
Add element to end |
Array.pop(arr) |
Remove and return last element |
Array.shift(arr) |
Remove and return first element |
Array.unshift(arr, val) |
Add element to start |
Array.slice(arr, from, to) |
Extract sub-array |
Array.concat(a, b) |
Concatenate two arrays |
Array.reverse(arr) |
Reverse array |
Array.sort(arr, cmp?) |
Sort array (optional comparator) |
Array.flat(arr) |
Flatten nested arrays |
Array.join(arr, sep) |
Join elements into string |
Functional Operations #
| Function | Description | Example |
|---|---|---|
Array.map(arr, fn) |
Transform each element | Array.map([1,2,3], |x| x * 2) → [2,4,6] |
Array.filter(arr, fn) |
Keep matching elements | Array.filter([1,2,3,4], |x| x > 2) → [3,4] |
Array.reduce(arr, fn, init) |
Reduce to single value | Array.reduce([1,2,3], |a,b| a + b, 0) → 6 |
Array.each(arr, fn) |
Execute for each element | Array.each(items, |x| print(x)) |
Array.find(arr, fn) |
Find first matching | Array.find([1,2,3], |x| x > 1) → 2 |
Array.find_index(arr, fn) |
Find index of first matching | Array.find_index([1,2,3], |x| x > 1) → 1 |
Array.contains(arr, val) |
Check if element exists | Array.contains([1,2,3], 2) → true |
Array.any(arr, fn) |
Check if any match | Array.any([1,2,3], |x| x > 2) → true |
Array.all(arr, fn) |
Check if all match | Array.all([1,2,3], |x| x > 0) → true |
Array.zip(a, b) |
Pair elements from two arrays | Array.zip([1,2], ["a","b"]) → [(1,"a"),(2,"b")] |
Array.enumerate(arr) |
Add indices | Array.enumerate(["a","b"]) → [(0,"a"),(1,"b")] |
Using with Pipe #
let result = [1, 2, 3, 4, 5]
|> Array.map(|x| x * x)
|> Array.filter(|x| x > 5)
|> Array.reduce(|a, b| a + b, 0)
// result = 9 + 16 + 25 = 50
Map Functions #
All map functions are in the Map namespace:
| Function | Description |
|---|---|
Map.new() |
Create empty map |
Map.from(tbl) |
Create from table |
Map.set(m, key, val) |
Set key-value pair |
Map.get(m, key) |
Get value by key |
Map.has(m, key) |
Check if key exists |
Map.remove(m, key) |
Remove key-value pair |
Map.keys(m) |
Get all keys as array |
Map.values(m) |
Get all values as array |
Map.entries(m) |
Get all key-value pairs |
Map.len(m) |
Get number of entries |
Map.each(m, fn) |
Iterate over entries |
Map.clear(m) |
Remove all entries |
Example #
let scores = Map.new()
Map.set(scores, "Alice", 95)
Map.set(scores, "Bob", 87)
print(Map.get(scores, "Alice")) // 95
print(Map.has(scores, "Carol")) // false
Map.each(scores, |key, val| {
print("{key}: {val}")
})
Set Functions #
All set functions are in the Set namespace:
| Function | Description |
|---|---|
Set.new() |
Create empty set |
Set.from(arr) |
Create from array |
Set.add(s, val) |
Add element |
Set.has(s, val) |
Check membership |
Set.remove(s, val) |
Remove element |
Set.len(s) |
Get size |
Set.to_array(s) |
Convert to array |
Set.union(a, b) |
Union of two sets |
Set.intersect(a, b) |
Intersection of two sets |
Set.difference(a, b) |
Elements in a but not b |
Set.each(s, fn) |
Iterate over elements |
Example #
let fruits = Set.from(["apple", "banana", "cherry"])
Set.add(fruits, "date")
print(Set.has(fruits, "apple")) // true
print(Set.len(fruits)) // 4
let tropical = Set.from(["banana", "mango", "papaya"])
let common = Set.intersect(fruits, tropical) // {"banana"}
Option Functions #
The Option type represents a value that may or may not exist:
| Function | Description |
|---|---|
Option.Some(val) |
Create Some with a value |
Option.None() |
Create None (no value) |
Option.is_some(opt) |
Check if has a value |
Option.is_none(opt) |
Check if empty |
Option.unwrap(opt) |
Extract value (errors if None) |
Option.unwrap_or(opt, default) |
Extract value or use default |
Option.map(opt, fn) |
Transform the value if present |
Option.and_then(opt, fn) |
Flat map (chain optionals) |
Option.or_else(opt, fn) |
Provide alternative if None |
Option.from(val) |
Create from value (nil → None) |
Example #
let maybe = Option.Some(42)
let nothing = Option.None()
print(Option.is_some(maybe)) // true
print(Option.unwrap_or(nothing, 0)) // 0
let doubled = Option.map(maybe, |x| x * 2)
print(Option.unwrap(doubled)) // 84
Result Functions #
The Result type represents success or failure:
| Function | Description |
|---|---|
Result.Ok(val) |
Create success |
Result.Err(err) |
Create error |
Result.is_ok(r) |
Check if success |
Result.is_err(r) |
Check if error |
Result.unwrap(r) |
Extract value (errors if Err) |
Result.unwrap_or(r, default) |
Extract value or use default |
Result.unwrap_err(r) |
Extract error value |
Result.map(r, fn) |
Transform success value |
Result.map_err(r, fn) |
Transform error value |
Result.and_then(r, fn) |
Chain results |
Result.or_else(r, fn) |
Handle error case |
Result.from_pcall(fn, ...) |
Create from protected call |
Example #
fn divide(a: int, b: int) -> Result<int, str> {
if b == 0 {
Result.Err("division by zero")
} else {
Result.Ok(a / b)
}
}
let result = divide(10, 2)
print(Result.unwrap(result)) // 5
let bad = divide(10, 0)
print(Result.is_err(bad)) // true
print(Result.unwrap_or(bad, -1)) // -1
Iterator Functions #
Lazy iterators for efficient data processing:
| Function | Description |
|---|---|
Iter.from(arr) |
Create from array |
Iter.range(start, stop, step?) |
Create range iterator |
Iter.map(iter, fn) |
Lazy map |
Iter.filter(iter, fn) |
Lazy filter |
Iter.take(iter, n) |
Take first n elements |
Iter.skip(iter, n) |
Skip first n elements |
Iter.zip(a, b) |
Zip two iterators |
Iter.enumerate(iter) |
Add indices |
Iter.fold(iter, init, fn) |
Reduce to single value |
Iter.collect(iter) |
Collect to array |
Iter.each(iter, fn) |
Execute for each |
Iter.count(iter) |
Count elements |
Iter.flat_map(iter, fn) |
Lazy flat map |
Iter.chain(a, b) |
Concatenate iterators |
Iter.find(iter, fn) |
Find first matching |
Iter.any(iter, fn) |
Check if any match |
Iter.all(iter, fn) |
Check if all match |
Example #
let result = Iter.range(0, 100)
|> Iter.map(|x| x * x)
|> Iter.filter(|x| x % 2 == 0)
|> Iter.take(5)
|> Iter.collect()
// [0, 4, 16, 36, 64]
Built-in Functions #
These functions are available globally:
| Function | Description |
|---|---|
print(...) |
Print to stdout |
type(val) |
Get type as string |
tostring(val) |
Convert to string |
tonumber(val) |
Convert to number |
assert_eq(a, b, msg?) |
Assert equality |
assert_ne(a, b, msg?) |
Assert inequality |
BigInt Support #
Zolo has native BigInt support for arbitrarily large integers:
let big = BigInt.from("99999999999999999999999")
let zero = BigInt.zero()
let one = BigInt.one()