titer_issues.nim 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. discard """
  2. output: '''
  3. 0
  4. 1
  5. 2
  6. 3
  7. 4
  8. 1
  9. start
  10. false
  11. 0
  12. 1
  13. 2
  14. end
  15. @[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42]
  16. 1002
  17. 0
  18. 1
  19. 2
  20. 7
  21. 9002
  22. 9004
  23. 9006
  24. 9008
  25. 9010
  26. 9012
  27. 9014
  28. 9016
  29. 9018
  30. @[1, 2]
  31. @[1, 2, 3]
  32. 1
  33. nested finally
  34. outer finally
  35. nested finally
  36. outer finally
  37. nested finally
  38. outer finally
  39. nested finally
  40. outer finally
  41. In defer
  42. trying
  43. exception caught
  44. finally block
  45. '''
  46. """
  47. import sequtils, strutils
  48. block t338:
  49. proc moo(): iterator (): int =
  50. iterator fooGen: int {.closure.} =
  51. while true:
  52. yield result
  53. result.inc
  54. return fooGen
  55. var foo = moo()
  56. for i in 0 .. 4:
  57. echo foo()
  58. block t8041:
  59. iterator xy[T](a: T, b: set[T]): T =
  60. if a in b:
  61. yield a
  62. for a in xy(1'i8, {}):
  63. for b in xy(a, {}):
  64. echo a
  65. block t3837_chained:
  66. iterator t1(): int {.closure.} =
  67. yield 1
  68. iterator t2(): int {.closure.} =
  69. for i in t1():
  70. yield i
  71. for i in t2():
  72. echo $i
  73. proc iter1(): (iterator: int) =
  74. let coll = [0,1,2]
  75. result = iterator: int {.closure.} =
  76. for i in coll:
  77. yield i
  78. proc iter2(it: (iterator: int)): (iterator: int) =
  79. result = iterator: int {.closure.} =
  80. echo finished(it)
  81. for i in it():
  82. yield i
  83. echo "start"
  84. let myiter1 = iter1()
  85. let myiter2 = iter2(myiter1)
  86. for i in myiter2():
  87. echo i
  88. echo "end"
  89. type Iterable[T] = (iterator: T) | Slice[T]
  90. ## Everything that can be iterated over, iterators and slices so far.
  91. proc toIter[T](s: Slice[T]): iterator: T =
  92. ## Iterate over a slice.
  93. iterator it: T {.closure.} =
  94. for x in s.a..s.b:
  95. yield x
  96. return it
  97. proc toIter[T](i: iterator: T): iterator: T =
  98. ## Nop
  99. i
  100. iterator map[T,S](i: Iterable[T], f: proc(x: T): S): S =
  101. let i = toIter(i)
  102. for x in i():
  103. yield f(x)
  104. proc filter[T](i: Iterable[T], f: proc(x: T): bool): iterator: T =
  105. let i = toIter(i)
  106. iterator it: T {.closure.} =
  107. for x in i():
  108. if f(x):
  109. yield x
  110. result = it
  111. iterator filter[T](i: Iterable[T], f: proc(x: T): bool): T =
  112. let i = toIter(i)
  113. for x in i():
  114. if f(x):
  115. yield x
  116. var it = toSeq(filter(2..10, proc(x: int): bool = x mod 2 == 0))
  117. doAssert it == @[2, 4, 6, 8, 10]
  118. it = toSeq(map(filter(2..10, proc(x: int): bool = x mod 2 == 0), proc(x: int): int = x * 2))
  119. doAssert it == @[4, 8, 12, 16, 20]
  120. block t3221_complex:
  121. iterator permutations[T](ys: openArray[T]): seq[T] =
  122. var
  123. d = 1
  124. c = newSeq[int](ys.len)
  125. xs = newSeq[T](ys.len)
  126. for i, y in ys: xs[i] = y
  127. yield xs
  128. block outer:
  129. while true:
  130. while d > 1:
  131. dec d
  132. c[d] = 0
  133. while c[d] >= d:
  134. inc d
  135. if d >= ys.len: break outer
  136. let i = if (d and 1) == 1: c[d] else: 0
  137. swap xs[i], xs[d]
  138. yield xs
  139. inc c[d]
  140. proc dig_vectors(): void =
  141. var v_nums: seq[int]
  142. v_nums = newSeq[int](1)
  143. for perm in permutations(toSeq(0 .. 1)):
  144. v_nums[0] = 1
  145. dig_vectors()
  146. block t3499_keepstate:
  147. proc slice[T](iter: iterator(): T {.closure.}, sl: auto): seq[T] =
  148. var res: seq[int64] = @[]
  149. var i = 0
  150. for n in iter():
  151. if i > sl.b:
  152. break
  153. if i >= sl.a:
  154. res.add(n)
  155. inc i
  156. res
  157. iterator harshad(): int64 {.closure.} =
  158. for n in 1 ..< int64.high:
  159. var sum = 0
  160. for ch in string($n):
  161. sum += parseInt("" & ch)
  162. if n mod sum == 0:
  163. yield n
  164. echo harshad.slice 0 ..< 20
  165. for n in harshad():
  166. if n > 1000:
  167. echo n
  168. break
  169. # bug #3499 last snippet fixed
  170. # bug #705 last snippet fixed
  171. block t1725_nested:
  172. iterator factory(): int {.closure.} =
  173. iterator bar(): int {.closure.} =
  174. yield 0
  175. yield 1
  176. yield 2
  177. for x in bar(): yield x
  178. for x in factory():
  179. echo x
  180. block t2023_objiter:
  181. type
  182. Obj = object
  183. iter: iterator (): int8 {.closure.}
  184. iterator test(): int8 {.closure.} =
  185. yield 7
  186. proc init():Obj=
  187. result.iter = test
  188. var o = init()
  189. echo(o.iter())
  190. block:
  191. # bug #13739
  192. iterator myIter(arg: openArray[int]): int =
  193. var tmp = 0
  194. let len = arg.len
  195. while tmp < len:
  196. yield arg[tmp] * 2
  197. inc tmp
  198. proc someProc() =
  199. var data = [4501,4502,4503,4504,4505,4506,4507,4508,4509]
  200. # StmtListExpr should not get special treatment.
  201. for x in myIter((discard;data)):
  202. echo x
  203. someProc()
  204. block:
  205. # bug #12576
  206. iterator ff(sq: varargs[seq[int]]): int =
  207. for x in sq:
  208. echo x
  209. for x in ff(@[1, 2], @[1, 2, 3]):
  210. echo x
  211. # bug #19575
  212. iterator bb() {.closure.} =
  213. while true:
  214. try: discard
  215. except: break
  216. finally: break
  217. var a = bb
  218. iterator cc() {.closure.} =
  219. while true:
  220. try: discard
  221. except:
  222. if true:
  223. break
  224. finally:
  225. if true:
  226. break
  227. var a2 = cc
  228. # bug #16876
  229. block:
  230. iterator a(num: int): int {.closure.} =
  231. if num == 1:
  232. yield num
  233. else:
  234. for i in a(num - 1):
  235. yield i
  236. for i in a(5):
  237. echo i
  238. block:
  239. # bug #19911 (return in nested try)
  240. # try yield -> try
  241. iterator p1: int {.closure.} =
  242. try:
  243. yield 0
  244. try:
  245. return
  246. finally:
  247. echo "nested finally"
  248. echo "shouldn't run"
  249. finally:
  250. echo "outer finally"
  251. echo "shouldn't run"
  252. for _ in p1():
  253. discard
  254. # try -> try yield
  255. iterator p2: int {.closure.} =
  256. try:
  257. try:
  258. yield 0
  259. return
  260. finally:
  261. echo "nested finally"
  262. echo "shouldn't run"
  263. finally:
  264. echo "outer finally"
  265. echo "shouldn't run"
  266. for _ in p2():
  267. discard
  268. # try yield -> try yield
  269. iterator p3: int {.closure.} =
  270. try:
  271. yield 0
  272. try:
  273. yield 0
  274. return
  275. finally:
  276. echo "nested finally"
  277. echo "shouldn't run"
  278. finally:
  279. echo "outer finally"
  280. echo "shouldn't run"
  281. for _ in p3():
  282. discard
  283. # try -> try
  284. iterator p4: int {.closure.} =
  285. try:
  286. try:
  287. return
  288. finally:
  289. echo "nested finally"
  290. echo "shouldn't run"
  291. finally:
  292. echo "outer finally"
  293. echo "shouldn't run"
  294. for _ in p4():
  295. discard
  296. # bug #18824
  297. iterator poc_iterator: int {.closure.} =
  298. block bug18824:
  299. try:
  300. break bug18824
  301. finally:
  302. echo "In defer"
  303. for _ in poc_iterator():
  304. discard
  305. # bug #20624
  306. iterator tryFinally() {.closure.} =
  307. block route:
  308. try:
  309. echo "trying"
  310. raise
  311. except:
  312. echo "exception caught"
  313. break route
  314. finally:
  315. echo "finally block"
  316. var x = tryFinally
  317. x()