tanoniter1.nim 448 B

12345678910111213141516171819202122232425262728293031323334
  1. discard """
  2. targets: "c js"
  3. output: '''1
  4. 2
  5. 3
  6. 4
  7. 1
  8. 2'''
  9. """
  10. proc factory(a, b: int): iterator (): int =
  11. iterator foo(): int {.closure.} =
  12. var x = a
  13. while x <= b:
  14. yield x
  15. inc x
  16. return foo
  17. proc factory2(a, b: int): iterator (): int =
  18. return iterator (): int =
  19. var x = a
  20. while x <= b:
  21. yield x
  22. inc x
  23. let foo = factory(1, 4)
  24. for f in foo():
  25. echo f
  26. let foo2 = factory2(1,2)
  27. for f in foo2(): echo f