tclosureiters.nim 2.2 KB

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