Code Examples

Thorn Code Examples

A collection of example programs demonstrating Thorn features. All examples are valid Thorn 0.3 syntax.


Hello World

write("hello world")

Variables & V-Strings

name = "Thorn"
version = 3
write(v"welcome to {name} v0.{version}")

Tasks

task circleArea(r) {
    need math
    send math.pi * r * r
}
area = circleArea(5)
write(v"area: {area}")

Loops

nums = [1, 2, 3, 4, 5]
loop(n = nums) {
    write(v"item: {n}")
}

loop(i = 10) {
    write(i)  # 0 through 9
}

Error Handling

err = risk {
    res = reach("https://api.example.com/data")
    send res.json
}
if (err != void) {
    write(v"request failed: {err}")
}

Full Program

need time
need math

task greet(name) {
    write(v"hello {name},
it is {now.hour}:{now.minute}")
}

section main {
    task run() {
        greet("world")
        area = math.pi * 5 * 5
        write(v"circle area: {area}")
    }
}

if (start) {
    main()
}

Quick Start Guide →