Examples
Hello world!
print "Hello world!"
let greet = [name] => print "Hello {name}!"
greet "world" //Hello world!
Add
let age = 28
let add = [a, b] => a + b
print add(3, 2) //5
print age:add(1) //29
Blast off!
for (10 to 0) [n] -> {
print n
}
print "Blast off!"
Fizzbuzz
let fizzbuzz = [n] => {
if (n % 3 == 0 && n % 5 == 0) return "FizzBuzz"
if (n % 5 == 0) return "Buzz"
if (n % 3 == 0) return "Fizz"
return n
}
for (0 to 10) print fizzbuzz(_)
let MultipleOf = [n] => [value] => value % n == 0
let fizzbuzz = [n] => n
let fizzbuzz = [:MultipleOf(3)] => "Fizz"
let fizzbuzz = [:MultipleOf(5)] => "Buzz"
let fizzbuzz = [:MultipleOf(3):and MultipleOf(5)] => "FizzBuzz"
for 0 to 10, print fizzbuzz(_)
Fibonacci
Naive:
let fibonacci = [n] => {
if (n <= 1) return 1
return fibonacci(n-1) + fibonacci(n-2)
}
Manual memoisation:
let cache = new UInt[]
let fibonacci = [n] => {
let cached = cache.(n)
if (cached != undefined) return cached
let result = fibonacci(n-1) - fibonacci(n-2)
cache.(n) = result
return result
}
Memoisation and overloading:
let fibonacci = memo [n] => fibonacci(n-1) + fibonacci(n-2)
let fibonacci = [:0] => 1
let fibonacci = [:1] => 1
Keydown
let keys = new []
on "keydown" [e] -> keys.(e.key) = true
on "keyup" [e] -> keys.(e.key) = false
let isKeyDown = [key] => keys.(key) == true
let events = new []
on "keydown" [e] -> {
let event = events.(e.key)
if (event != undefined) event(e)
}
events.(" ") = [] -> print("Space bar was pressed")
Last updated
Was this helpful?