titer_issues.nim 4.2 KB

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