tclosureiters.nim 2.6 KB

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