titer_issues.nim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. '''
  33. """
  34. import sequtils, strutils
  35. block t338:
  36. proc moo(): iterator (): int =
  37. iterator fooGen: int {.closure.} =
  38. while true:
  39. yield result
  40. result.inc
  41. return fooGen
  42. var foo = moo()
  43. for i in 0 .. 4:
  44. echo foo()
  45. block t8041:
  46. iterator xy[T](a: T, b: set[T]): T =
  47. if a in b:
  48. yield a
  49. for a in xy(1'i8, {}):
  50. for b in xy(a, {}):
  51. echo a
  52. block t3837_chained:
  53. iterator t1(): int {.closure.} =
  54. yield 1
  55. iterator t2(): int {.closure.} =
  56. for i in t1():
  57. yield i
  58. for i in t2():
  59. echo $i
  60. proc iter1(): (iterator: int) =
  61. let coll = [0,1,2]
  62. result = iterator: int {.closure.} =
  63. for i in coll:
  64. yield i
  65. proc iter2(it: (iterator: int)): (iterator: int) =
  66. result = iterator: int {.closure.} =
  67. echo finished(it)
  68. for i in it():
  69. yield i
  70. echo "start"
  71. let myiter1 = iter1()
  72. let myiter2 = iter2(myiter1)
  73. for i in myiter2():
  74. echo i
  75. echo "end"
  76. type Iterable[T] = (iterator: T) | Slice[T]
  77. ## Everything that can be iterated over, iterators and slices so far.
  78. proc toIter[T](s: Slice[T]): iterator: T =
  79. ## Iterate over a slice.
  80. iterator it: T {.closure.} =
  81. for x in s.a..s.b:
  82. yield x
  83. return it
  84. proc toIter[T](i: iterator: T): iterator: T =
  85. ## Nop
  86. i
  87. iterator map[T,S](i: Iterable[T], f: proc(x: T): S): S =
  88. let i = toIter(i)
  89. for x in i():
  90. yield f(x)
  91. proc filter[T](i: Iterable[T], f: proc(x: T): bool): iterator: T =
  92. let i = toIter(i)
  93. iterator it: T {.closure.} =
  94. for x in i():
  95. if f(x):
  96. yield x
  97. result = it
  98. iterator filter[T](i: Iterable[T], f: proc(x: T): bool): T =
  99. let i = toIter(i)
  100. for x in i():
  101. if f(x):
  102. yield x
  103. var it = toSeq(filter(2..10, proc(x: int): bool = x mod 2 == 0))
  104. doAssert it == @[2, 4, 6, 8, 10]
  105. it = toSeq(map(filter(2..10, proc(x: int): bool = x mod 2 == 0), proc(x: int): int = x * 2))
  106. doAssert it == @[4, 8, 12, 16, 20]
  107. block t3221_complex:
  108. iterator permutations[T](ys: openarray[T]): seq[T] =
  109. var
  110. d = 1
  111. c = newSeq[int](ys.len)
  112. xs = newSeq[T](ys.len)
  113. for i, y in ys: xs[i] = y
  114. yield xs
  115. block outer:
  116. while true:
  117. while d > 1:
  118. dec d
  119. c[d] = 0
  120. while c[d] >= d:
  121. inc d
  122. if d >= ys.len: break outer
  123. let i = if (d and 1) == 1: c[d] else: 0
  124. swap xs[i], xs[d]
  125. yield xs
  126. inc c[d]
  127. proc dig_vectors(): void =
  128. var v_nums: seq[int]
  129. v_nums = newSeq[int](1)
  130. for perm in permutations(toSeq(0 .. 1)):
  131. v_nums[0] = 1
  132. dig_vectors()
  133. block t3499_keepstate:
  134. proc slice[T](iter: iterator(): T {.closure.}, sl: auto): seq[T] =
  135. var res: seq[int64] = @[]
  136. var i = 0
  137. for n in iter():
  138. if i > sl.b:
  139. break
  140. if i >= sl.a:
  141. res.add(n)
  142. inc i
  143. res
  144. iterator harshad(): int64 {.closure.} =
  145. for n in 1 ..< int64.high:
  146. var sum = 0
  147. for ch in string($n):
  148. sum += parseInt("" & ch)
  149. if n mod sum == 0:
  150. yield n
  151. echo harshad.slice 0 ..< 20
  152. for n in harshad():
  153. if n > 1000:
  154. echo n
  155. break
  156. # bug #3499 last snippet fixed
  157. # bug #705 last snippet fixed
  158. block t1725_nested:
  159. iterator factory(): int {.closure.} =
  160. iterator bar(): int {.closure.} =
  161. yield 0
  162. yield 1
  163. yield 2
  164. for x in bar(): yield x
  165. for x in factory():
  166. echo x
  167. block t2023_objiter:
  168. type
  169. Obj = object
  170. iter: iterator (): int8 {.closure.}
  171. iterator test(): int8 {.closure.} =
  172. yield 7
  173. proc init():Obj=
  174. result.iter = test
  175. var o = init()
  176. echo(o.iter())
  177. block:
  178. # bug #13739
  179. iterator myIter(arg: openarray[int]): int =
  180. var tmp = 0
  181. let len = arg.len
  182. while tmp < len:
  183. yield arg[tmp] * 2
  184. inc tmp
  185. proc someProc() =
  186. var data = [4501,4502,4503,4504,4505,4506,4507,4508,4509]
  187. # StmtListExpr should not get special treatment.
  188. for x in myIter((discard;data)):
  189. echo x
  190. someProc()
  191. block:
  192. # bug #12576
  193. iterator ff(sq: varargs[seq[int]]): int =
  194. for x in sq:
  195. echo x
  196. for x in ff(@[1, 2], @[1, 2, 3]):
  197. echo x