Example Programs
A gallery of TinyLang programs demonstrating the language's features. Each example is a complete, runnable program. Find these in the examples/ directory.
01 - Hello World
Your first program. Demonstrates strings, variables, and the print function.
// Hello World - Your first TinyLang program!
print("Hello, World!")
print("Welcome to TinyLang 🎉")
// Variables
let name = "TinyLang"
let version = 1.0
print("I am " + name + " version " + str(version))
print("I was built to help you learn programming!")
# Run with: tinylang run examples/01-hello.tiny
02 - Variables and Data Types
Demonstrates all six data types: numbers, strings, booleans, null, arrays, and objects.
// Numbers (integers and floats)
let age = 25
let pi = 3.14159
let negative = -42
// Strings
let greeting = "Hello"
let name = 'World'
let message = greeting + ", " + name + "!"
// Booleans and Null
let isStudent = true
let nothing = null
// Arrays
let colors = ["red", "green", "blue"]
print("First color:", colors[0])
// Objects
let person = {name: "Alice", age: 30, city: "NYC"}
print("Name:", person.name)
// Constants (cannot be reassigned)
const MAX_SIZE = 100
const APP_NAME = "TinyLang"
// Type checking
print("Type of age:", type(age)) // "number"
print("Type of colors:", type(colors)) // "array"
03 - Functions and Closures
Demonstrates named functions, arrow functions, default parameters, closures, and higher-order functions.
// Basic function
fn add(a, b) {
return a + b
}
// Default parameters
fn greet(name, greeting = "Hello") {
return greeting + ", " + name + "!"
}
print(greet("Alice")) // Hello, Alice!
print(greet("Bob", "Hi")) // Hi, Bob!
// Arrow functions
let double = (x) => x * 2
let square = (x) => x * x
// Closures - capture their environment
fn makeCounter(start = 0) {
let count = start
return fn() {
count += 1
return count
}
}
let counter = makeCounter()
print(counter()) // 1
print(counter()) // 2
print(counter()) // 3
// Function composition
fn compose(f, g) {
return (x) => f(g(x))
}
let doubleSquare = compose(double, square)
print("double(square(3)) =", doubleSquare(3)) // 18
04 - Arrays and Higher-Order Functions
Demonstrates array operations, map/filter/reduce, method chaining, and functional patterns.
let nums = [5, 2, 8, 1, 9, 3]
// Sorting and reversing
print("Sorted:", sort(nums)) // [1, 2, 3, 5, 8, 9]
print("Reversed:", reverse(nums)) // [3, 9, 1, 8, 2, 5]
// Map - transform each element
let doubled = nums.map((x) => x * 2)
print("Doubled:", doubled)
// Filter - keep matching elements
let evens = nums.filter((x) => x % 2 == 0)
print("Evens:", evens)
// Reduce - combine into single value
let sum = nums.reduce((acc, x) => acc + x, 0)
print("Sum:", sum)
// Chaining operations
let result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.filter((x) => x % 2 == 0)
.map((x) => x * x)
print("Even squares:", result) // [4, 16, 36, 64, 100]
05 - Loops and Control Flow
Demonstrates while loops, for..in loops, ranges, break, and continue.
// While loop - countdown
let n = 5
while n > 0 {
print(n)
n -= 1
}
print("Liftoff! 🚀")
// For loop with range
for i in 1..6 {
print(str(i) + "² = " + str(i * i))
}
// For loop with array
let items = ["milk", "eggs", "bread"]
for item in items {
print(" ☐ " + item)
}
// Break - exit early
for i in 1..20 {
if i > 5 and i % 2 == 0 {
print("Found:", i)
break
}
}
// Continue - skip iteration
for i in 1..11 {
if i % 2 == 0 { continue }
print(i) // Only odd numbers
}
06 - Classes and Inheritance
Demonstrates classes, constructors, methods, inheritance, and polymorphism.
class Animal {
let name = ""
let sound = ""
fn init(name, sound) {
this.name = name
this.sound = sound
}
fn speak() {
print(this.name + " says '" + this.sound + "'!")
}
}
class Dog extends Animal {
let tricks = []
fn init(name) {
this.name = name
this.sound = "Woof"
this.tricks = []
}
fn learn(trick) {
this.tricks.push(trick)
print(this.name + " learned: " + trick)
}
}
let buddy = new Dog("Buddy")
buddy.speak() // Buddy says 'Woof'!
buddy.learn("sit") // Buddy learned: sit
buddy.learn("shake") // Buddy learned: shake
07 - Fibonacci (Multiple Approaches)
Demonstrates recursion, iteration, and arrays through the classic Fibonacci problem.
// Recursive approach
fn fib_recursive(n) {
if n <= 1 { return n }
return fib_recursive(n - 1) + fib_recursive(n - 2)
}
// Iterative approach (faster)
fn fib_iterative(n) {
if n <= 1 { return n }
let a = 0
let b = 1
for i in 2..n + 1 {
let temp = b
b = a + b
a = temp
}
return b
}
// Generate entire sequence
fn fib_sequence(count) {
let seq = [0, 1]
for i in 2..count {
let next = seq[i - 1] + seq[i - 2]
push(seq, next)
}
return seq
}
print("fib(10) =", fib_recursive(10)) // 55
print("fib(30) =", fib_iterative(30)) // 832040
print("First 15:", fib_sequence(15))
08 - Sorting Algorithms
Implements classic sorting algorithms: bubble sort, selection sort, and insertion sort.
fn bubbleSort(arr) {
let n = len(arr)
let sorted = slice(arr, 0, n)
for i in 0..n {
for j in 0..n - i - 1 {
if sorted[j] > sorted[j + 1] {
let temp = sorted[j]
sorted[j] = sorted[j + 1]
sorted[j + 1] = temp
}
}
}
return sorted
}
let data = [64, 34, 25, 12, 22, 11, 90]
print("Original:", data)
print("Sorted:", bubbleSort(data))
09 - Functional Programming
Demonstrates function composition, pipes, currying patterns, and data pipelines.
// Function composition
fn compose(f, g) {
return (x) => f(g(x))
}
let double = (x) => x * 2
let addOne = (x) => x + 1
let doubleAndAdd = compose(addOne, double)
print(doubleAndAdd(5)) // 11
// Currying pattern
fn multiplier(factor) {
return (x) => x * factor
}
let triple = multiplier(3)
print(triple(7)) // 21
// Data pipeline with map/filter/reduce
let people = [
{name: "Alice", age: 30, city: "NYC"},
{name: "Bob", age: 25, city: "LA"},
{name: "Charlie", age: 35, city: "NYC"}
]
// Get names of NYC residents over 28
let nycPeople = people
.filter((p) => p.city == "NYC")
.filter((p) => p.age > 28)
.map((p) => p.name)
print("NYC over 28:", nycPeople) // ["Alice", "Charlie"]
10 - Pattern Matching
Demonstrates the match/when expression for clean multi-way branching.
fn dayType(day) {
match day {
when "Saturday" => return "weekend 🎉"
when "Sunday" => return "weekend 🎉"
when "Friday" => return "almost done! 🙌"
else => return "weekday 📝"
}
}
let days = ["Monday", "Friday", "Saturday"]
for day in days {
print(day + ": " + dayType(day))
}
// Calculator with match
fn calculate(op, a, b) {
match op {
when "+" => return a + b
when "-" => return a - b
when "*" => return a * b
when "/" => {
if b == 0 { return null }
return a / b
}
else => return null
}
}
print("10 + 3 =", calculate("+", 10, 3)) // 13
print("10 * 3 =", calculate("*", 10, 3)) // 30
Running Examples
All examples are in the examples/ directory. Run any of them with:
tinylang run examples/01-hello.tiny
tinylang run examples/07-fibonacci.tiny
tinylang run examples/09-functional.tiny
Or compile and run on the VM for better performance:
tinylang compile examples/08-sorting.tiny
tinylang exec examples/08-sorting.tinyc