jorts/examples/higher-order-functions.jt

27 lines
666 B
Text
Raw Normal View History

2019-03-08 21:30:30 +00:00
import io
// takes a function that receives two ints, returns an int
// Func is the function type keyword, to not switch it with fn (which declares
// a function)
fn function_tester (Func function ([int, int] -> int)) -> int {
func(2, 2)
}
fn add(int a, int b) -> int {
a + b
}
fn main () {
// passes the function add to function_tester
res := function_tester(add)
// you can also create functions and put them in variables. not putting a
// function name on the fn block makes it return a Func instance to be put
// in a variable
anonymous := (fn () {})
// anonymous has type Func ([] -> void)
io.puts("res = {res}")
}