tclosureiters.nim 2.6 KB

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