Examples
Explore practical Zolo language examples. Click any example to open in the interactive playground.
26 examples 8 categories
hello.zolo
Basicslet name = "Zolo"
let version = 1
print("Hello, {name} v{version}!")
print("The modern language with a VM written in Rust") // output
Hello, Zolo v1! The modern language with a VM written in Rust
variables.zolo
Basics// let: immutable, const: constant, mut: mutable
let name: str = "Zolo"
const PI: float = 3.14159
let mut counter = 0
counter = counter + 1
counter = counter + 1
print("Name: {name}")
print("PI: {PI}")
print("Counter: {counter}")
... // output
Name: Zolo PI: 3.14159
pipe.zolo
Basics// The pipe |> passes the result as the first argument
fn double(x: int) -> int { x * 2 }
fn add_ten(x: int) -> int { x + 10 }
fn square(x: int) -> int { x * x }
let result = 5
|> double()
|> add_ten()
|> square()
print(result) // ((5*2)+10)^2 = 400
... // output
400 Hello, World
pattern-matching.zolo
Control Flowfn classify(n: int) -> str {
match n {
0 => "zero",
1..=9 => "single digit",
10..=99 => "double digit",
_ => "large",
}
}
fn day_name(d: int) -> str {
match d {
1 => "Monday",
... // output
zero single digit
iterators.zolo
Iterators// Lazy iterators — only compute what is needed
// Even squares of the first 5 numbers
let result = 0..
|> Iter.map(|x| x * x)
|> Iter.filter(|x| x % 2 == 0)
|> Iter.take(5)
|> Iter.collect()
print(result)
// Fibonacci via Iter.from_fn
... // output
[0, 4, 16, 36, 64] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
structs.zolo
Structuresstruct Point {
x: float,
y: float,
}
impl Point {
fn new(x: float, y: float) -> Point {
return Point { x, y }
}
fn distance(self, other: Point) -> float {
let dx = self.x - other.x
... // output
A: (0, 0) B: (3, 4)
enums.zolo
Structuresenum Shape {
Circle(float),
Rect(float, float),
Triangle(float, float, float),
}
fn area(shape: Shape) -> float {
match shape {
Shape.Circle(r) => 3.14159 * r * r,
Shape.Rect(w, h) => w * h,
Shape.Triangle(a, b, c) => {
let s = (a + b + c) / 2.0
... // output
Circle with radius 5: area = 78.53975 Rectangle 4x6: area = 24
null-safety.zolo
Safetystruct Address {
street: str,
city: str?,
}
struct User {
name: str,
address: Address?,
}
let user = User {
name: "Alice",
... // output
Alice: Springfield Guest: Unknown
decorators.zolo
Advanced// @memoize: automatic result caching
@memoize
fn fibonacci(n: int) -> int {
if n <= 1 { return n }
return fibonacci(n - 1) + fibonacci(n - 2)
}
// @test: inline tests
@test
fn test_fibonacci() {
assert_eq(fibonacci(0), 0)
assert_eq(fibonacci(1), 1)
... // output
fib(30) = 832040 fib(35) = 9227465
closures.zolo
Functions// Closures capture variables from the outer scope
fn make_counter(start: int) {
let mut count = start
return || {
count = count + 1
return count
}
}
let counter = make_counter(0)
print(counter()) // 1
print(counter()) // 2
... // output
1 2
array-ops.zolo
Structureslet nums = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
// Basic operations
print("Length: {Array.len(nums)}")
print("First: {nums[0]}")
print("Last: {nums[Array.len(nums) - 1]}")
// Functional
let evens = nums |> Array.filter(|x| x % 2 == 0)
let doubled = nums |> Array.map(|x| x * 2)
let sum = nums |> Array.reduce(|acc, x| acc + x, 0)
... // output
Length: 10 First: 3
generators.zolo
Advanced// Generators with fn* and yield
fn* range(start: int, end: int, step: int) {
let mut i = start
while i < end {
yield i
i = i + step
}
}
fn* fibonacci() {
let mut a = 0
let mut b = 1
... // output
0 3
loops.zolo
Control Flow// for with exclusive and inclusive ranges
print("Range 0..5:")
for i in 0..5 {
print(i)
}
print("Range 1..=3:")
for i in 1..=3 {
print(i)
}
// for over array
... // output
Range 0..5: 0
result.zolo
Safetyfn divide(a: int, b: int) -> Result<int, str> {
if b == 0 {
return Result.Err("division by zero")
}
return Result.Ok(a / b)
}
// Checking and extracting
let r1 = divide(10, 2)
let r2 = divide(5, 0)
print(r1.is_ok()) // true
... // output
true true
traits.zolo
Structurestrait Displayable {
fn display(self) -> str
// Default implementation
fn debug(self) -> str {
return "[debug] {self.display()}"
}
}
struct Circle {
radius: float,
}
... // output
Circle(r=5) [debug] Circle(r=5)
destructuring.zolo
Functions// Tuples — destructuring directly in let
fn min_max(nums: [int]) -> (int, int) {
let mut lo = nums[0]
let mut hi = nums[0]
for n in nums {
if n < lo { lo = n }
if n > hi { hi = n }
}
return (lo, hi)
}
let (min, max) = min_max([3, 1, 4, 1, 5, 9, 2, 6])
... // output
Min: 1, Max: 9 17 / 5 = 3 remainder 2
maps.zolo
Structures// Map literal uses #{...} to distinguish from blocks
let scores: {str: int} = #{
"Alice": 95,
"Bob": 87,
"Carol": 92,
}
// Access by key or by field
print("Alice: {scores["Alice"]}")
print("Bob: {scores.Bob}")
// Iterate with (key, value) — destructuring directly in for
... // output
Alice: 95 Bob: 87
strings.zolo
Basics// String methods
let s = " Hello, World! "
print(s.trim())
print(s.trim().upper())
print(s.trim().lower())
print(s.trim().len())
// Search and check
let text = "Zolo is fast and safe"
print(text.contains("fast"))
print(text.starts_with("Zolo"))
print(text.ends_with("safe"))
... // output
Hello, World! HELLO, WORLD!
string-format.zolo
Basics// Basic interpolation
let name = "Zolo"
let version = 1
print("Language: {name} v{version}")
// Expressions inside {}
let price = 9.99
let qty = 3
print("Total: $ {price * qty:.2f}")
// Floats
let pi = 3.14159265
... // output
Language: Zolo v1 Total: $ 29.97
match-guards.zolo
Control Flow// Guards with if in match arms
fn classify(n: int) -> str {
match n {
n if n < 0 => "negative",
0 => "zero",
n if n % 2 == 0 && n < 100 => "small even",
n if n % 2 != 0 && n < 100 => "small odd",
n if n >= 100 => "large ({n})",
_ => "unknown",
}
}
... // output
-5 => negative 0 => zero
tap-spread.zolo
Advanced// dbg(): helper that prints the array in debug format and returns nil
fn dbg(x: [int]) { print("{x:?}") }
// Tap operator &. — side effect without breaking the chain
let result = [5, 2, 8, 1, 9, 3]
&. dbg() // prints the original array
|> Array.filter(|x| x > 3)
&. dbg() // prints after filtering
|> Array.map(|x| x * x)
&. dbg() // prints after mapping
|> Array.sort()
... // output
[5, 2, 8, 1, 9, 3] [5, 8, 9]
if-expression.zolo
Control Flow// if returns a value — can be used in any expression
let score = 85
let grade = if score >= 90 { "A" }
else if score >= 80 { "B" }
else if score >= 70 { "C" }
else { "F" }
print("Grade: {grade}")
// Inline in interpolation
let temp = 38.5
... // output
Grade: B Temperature 38.5°C: fever
higher-order.zolo
Functions// Functions that take and return functions
fn compose(f: fn(int) -> int, g: fn(int) -> int) -> fn(int) -> int {
return |x| f(g(x))
}
fn memoize(f: fn(int) -> int) -> fn(int) -> int {
let cache: {int: int} = {}
return |n| {
if let hit = cache[n] { return hit }
let result = f(n)
cache[n] = result
return result
... // output
11 25
http-server.zolo
Advanced// HTTP routing in Zolo is composed via pipes.
// This example shows the structure — to serve for real,
// save as server.zolo and run 'zolo run server.zolo' in the native CLI.
struct Route {
method: str,
path: str,
handler: str,
}
// Handlers (on a real server, they take req and return string/map)
fn handle_root(_req: str) -> str { return "Hello from Zolo!" }
... // output
Registered routes: GET / => handle_root
canvas-graphics.zolo
Graphics// Canvas plugin — 2D software rasterizer (96x64 pixels)
use plugin canvas::{Canvas}
let c = Canvas.new(96, 64)
// Dark background
c.fill(16, 18, 32, 255)
// Filled rectangle — teal panel
c.fill_rect(4, 4, 88, 20, 20, 80, 100, 255)
// Title text
... // output
rendered 96x64
database.zolo
Advanced// Database and SQL in Zolo: canonical SQL + per-dialect transpilation.
// The actual connection (Database.open) requires the native CLI — here we show
// the *modeling* of the layer: rows as structs, queries as data.
struct Product {
id: int,
name: str,
price: float,
}
let inventory = [
Product { id: 1, name: "Widget", price: 9.99 },
... // output
Top 2 by price: Gadget: 19.99
Want to learn step by step?
The interactive Tutorial guides you through the language with practical exercises and immediate feedback.
Start the Tutorial →