talc/examples/closures.talc

24 lines
342 B
Text
Executable file

#!/usr/bin/env talc
-- adapted from Crafting Interpreters 10.6
make_counter = \-> do
var i = 0
\-> do
i += 1
println(i)
end
end
var counter1 = make_counter()
counter1() -- 1
counter1() -- 2
counter1() -- 3
counter1() -- 4
var counter2 = make_counter()
counter2() -- 1
counter2() -- 2
counter1() -- 5
counter1() -- 6
counter2() -- 3