tomo/test/lambdas.tm

34 lines
605 B
Plaintext
Raw Normal View History

2024-03-09 11:03:42 -08:00
>> add_one := func(x:Int) x + 1
>> add_one(10)
= 11
2024-03-09 11:09:18 -08:00
>> shout := func(msg:Text) say("{msg:upper()}!")
>> shout("hello")
2024-03-09 11:12:15 -08:00
>> asdf := add_one
>> asdf(99)
= 100
2024-03-09 13:03:38 -08:00
func make_adder(x:Int)-> func(y:Int)->Int
return func(y:Int) x + y
>> add_100 := make_adder(100)
>> add_100(5)
= 105
func suffix_fn(fn:func(t:Text)->Text, suffix:Text)->func(t:Text)->Text
return func(t:Text) fn(t)++suffix
>> shout2 := suffix_fn(Text.upper, "!")
>> shout2("hello")
= "HELLO!"
2024-03-09 13:08:03 -08:00
func mul_func(n:Int, fn:func(x:Int)->Int)-> func(x:Int)->Int
return func(x:Int) n*fn(x)
>> abs100 := mul_func(100, Int.abs)
>> abs100(-5)
= 500