jorts/examples/higher-order-functions.jt

27 lines
662 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 func ([int, int] -> int)) -> int {
2019-03-08 21:30:30 +00:00
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}")
}