titer_issues.nim 3.8 KB

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