tclosureiters.nim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. discard """
  2. output: '''0
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 6
  9. 7
  10. 8
  11. 9
  12. 10
  13. 5 5
  14. 7 7
  15. 9 9
  16. 0
  17. 0
  18. 0
  19. 0
  20. 1
  21. 2
  22. 70
  23. 0
  24. '''
  25. """
  26. when true:
  27. proc main() =
  28. let
  29. lo=0
  30. hi=10
  31. iterator itA(): int =
  32. for x in lo..hi:
  33. yield x
  34. for x in itA():
  35. echo x
  36. var y: int
  37. iterator itB(): int =
  38. while y <= hi:
  39. yield y
  40. inc y
  41. y = 5
  42. for x in itB():
  43. echo x, " ", y
  44. inc y
  45. main()
  46. iterator infinite(): int {.closure.} =
  47. var i = 0
  48. while true:
  49. yield i
  50. inc i
  51. iterator take[T](it: iterator (): T, numToTake: int): T {.closure.} =
  52. var i = 0
  53. for x in it():
  54. if i >= numToTake:
  55. break
  56. yield x
  57. inc i
  58. # gives wrong reasult (3 times 0)
  59. for x in infinite.take(3):
  60. echo x
  61. # does what we want
  62. let inf = infinite
  63. for x in inf.take(3):
  64. echo x
  65. # bug #3583
  66. proc foo(f: (iterator(): int)) =
  67. for i in f(): echo i
  68. let fIt = iterator(): int = yield 70
  69. foo fIt
  70. # bug #5321
  71. proc lineIter*(filename: string): iterator(): string =
  72. result = iterator(): string {.closure.} =
  73. for line in lines(filename):
  74. yield line
  75. proc unused =
  76. var count = 0
  77. let iter = lineIter("temp10.nim")
  78. for line in iter():
  79. count += 1
  80. iterator lineIter2*(filename: string): string {.closure.} =
  81. var f = open(filename, bufSize=8000)
  82. defer: close(f) # <-- commenting defer "solves" the problem
  83. var res = TaintedString(newStringOfCap(80))
  84. while f.readLine(res): yield res
  85. proc unusedB =
  86. var count = 0
  87. for line in lineIter2("temp10.nim"):
  88. count += 1
  89. # bug #5519
  90. import os, algorithm
  91. iterator filesIt(path: string): auto {.closure.} =
  92. var files = newSeq[string]()
  93. var dirs = newSeq[string]()
  94. for k, p in os.walkDir(path):
  95. let (_, n, e) = p.splitFile
  96. if e != "":
  97. continue
  98. case k
  99. of pcFile, pcLinkToFile:
  100. files.add(n)
  101. else:
  102. dirs.add(n)
  103. files.sort(system.cmp)
  104. dirs.sort(system.cmp)
  105. for f in files:
  106. yield f
  107. for d in dirs:
  108. files = newSeq[string]()
  109. for k, p in os.walkDir(path / d):
  110. let (_, n, e) = p.splitFile
  111. if e != "":
  112. continue
  113. case k
  114. of pcFile, pcLinkToFile:
  115. files.add(n)
  116. else:
  117. discard
  118. files.sort(system.cmp)
  119. let prefix = path.splitPath[1]
  120. for f in files:
  121. yield prefix / f
  122. # bug #13815
  123. var love = iterator: int {.closure.} =
  124. yield cast[type(
  125. block:
  126. var a = 0
  127. yield a
  128. a)](0)
  129. for i in love():
  130. echo i