dev
Download
36 lines 876 BTomoModified 2025-09-25 raw
123456789101112131415161718192021222324252627282930313233343536
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
	assert add_one(10) == 11

	>> shout := func(msg:Text) say("$(msg.upper())!")
	>> shout("hello")

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

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

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

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

	# Test nested lambdas:
    outer := "Hello"
    fn := func()
        return func()
            return func()
                defer say("$outer")
                return outer
    assert fn()()() == "Hello"