titer3.nim 739 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. discard """
  2. output: '''1231
  3. 4
  4. 6
  5. 8
  6. --------
  7. 4
  8. 6
  9. 8
  10. '''
  11. """
  12. iterator count1_3: int =
  13. yield 1
  14. yield 2
  15. yield 3
  16. for x in count1_3():
  17. write(stdout, $x)
  18. # yield inside an iterator, but not in a loop:
  19. iterator iter1(a: openArray[int]): int =
  20. yield a[0]
  21. var x = [[1, 2, 3], [4, 5, 6]]
  22. for y in iter1(x[0]): write(stdout, $y)
  23. writeLine(stdout, "")
  24. # ensure closure and inline iterators have the same behaviour regarding
  25. # parameter passing
  26. iterator clo(a: int): int {.closure.} =
  27. yield 0+a
  28. yield 1+a
  29. yield 2+a
  30. iterator inl(a: int): int {.inline.} =
  31. yield 0+a
  32. yield 1+a
  33. yield 2+a
  34. proc main =
  35. var y = 4
  36. for i in clo(y):
  37. echo i
  38. inc y
  39. echo "--------"
  40. y = 4
  41. for i in inl(y):
  42. echo i
  43. inc y
  44. main()