tanoniter1.nim 430 B

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