aboutsummaryrefslogtreecommitdiff
path: root/test/lambdas.tm
blob: 40f24abb7c1a7d8693bbad6bce40bb253461f74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
func make_adder(x:Int)-> func(y:Int)->Int:
	return func(y:Int): x + y

func suffix_fn(fn:func(t:Text)->Text, suffix:Text)->func(t:Text)->Text:
	return func(t:Text): fn(t)++suffix

func mul_func(n:Int, fn:func(x:Int)->Int)-> func(x:Int)->Int:
	return func(x:Int): n*fn(x)

func main():
	>> add_one := func(x:Int): x + 1
	>> add_one(10)
	= 11

	>> shout := func(msg:Text): say("{msg:upper()}!")
	>> shout("hello")

	>> asdf := add_one
	>> asdf(99)
	= 100

	>> add_100 := make_adder(100)
	>> add_100(5)
	= 105

	>> shout2 := suffix_fn(Text.upper, "!")
	>> shout2("hello")
	= "HELLO!"

	>> abs100 := mul_func(100, Int.abs)
	>> abs100(-5)
	= 500

	// Test nested lambdas:
    outer := "Hello"
    fn := func():
        return func():
            return func():
                defer: //! {outer}
                return outer
    >> fn()()()
	= "Hello"