Closure.k 429 B

123456789101112131415161718192021
  1. invoke {
  2. function adder (n: Int) -> Function {
  3. return lambda (x: Int) -> Int {
  4. return n + x
  5. }
  6. }
  7. assert adder(5) is Function
  8. assert adder(7)(9) == 16
  9. function counter () -> Function {
  10. var n = 0
  11. return lambda () -> Int {
  12. reset n += 1
  13. return n
  14. }
  15. }
  16. let c = counter()
  17. assert c() == 1
  18. assert c() == 2
  19. assert c() == 3
  20. }