Practical Examples
Collection of practical examples in JARU to help you get started with the language.
Hello World
The most basic example to get started:
println("Hello World!")
Basic Calculator
A simple calculator with basic operations:
func calculator(a, b, operation)
switch (operation)
case "+":
return a + b
case "-":
return a - b
case "*":
return a * b
case "/":
if (b != 0) then
return a / b
else
return "Error: Division by zero"
end
default:
return "Invalid operation"
end
end
println(calculator(10, 5, "+")) // 15
println(calculator(10, 5, "-")) // 5
println(calculator(10, 5, "*")) // 50
println(calculator(10, 5, "/")) // 2
LED Control (ESP32)
Example of how to control an LED with GPIO:
use GPIO
// Configure pin 2 as output (built-in LED on many ESP32)
GPIO.pin(2, GPIO.OUTPUT)
// Blink the LED
while (true)
GPIO.write(GPIO.HIGH)
pause(1000)
GPIO.write(GPIO.LOW)
pause(1000)
end
Sensor Reading
Example of reading an analog sensor:
use GPIO
GPIO.pin(34, GPIO.INPUT)
while (true)
var value = GPIO.analogRead(sensor)
println("Sensor value: ", value)
pause(500)
end
Using Classes
Example of object-oriented programming:
class Vehicle
def init(brand, model)
this.brand = brand
this.model = model
this.running = false
end
def start()
this.running = true
println(this.brand, " ", this.model, " started")
end
def stop()
this.running = false
println(this.brand, " ", this.model, " stopped")
end
end
class Car : Vehicle
def init(brand, model, doors)
this.brand = brand
this.model = model
this.doors = doors
this.running = false
end
def info()
println("Car: ", this.brand, " ", this.model)
println("Doors: ", this.doors)
end
end
var myCar = Car("Toyota", "Corolla", 4)
myCar.info()
myCar.start()
List Handling
Example of working with lists:
var fruits = ["apple", "pear", "orange"]
// Add element
fruits.append("banana")
// Iterate through the list
foreach (var fruit in fruits)
println(fruit)
end
// Access by index
println("First fruit: ", fruits[0])
Recursive Fibonacci
Example of recursion calculating Fibonacci numbers:
func fibonacci(n)
if (n <= 1) then
return n
else
return fibonacci(n - 1) + fibonacci(n - 2)
end
end
for (var i = 0; i < 10; i++)
print(fibonacci(i), " ")
end
// Output: 0 1 1 2 3 5 8 13 21 34