tyield.nim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. targets: "c cpp js"
  4. """
  5. import std/[sugar, algorithm]
  6. import std/assertions
  7. block:
  8. var x = @[(6.0, 6, '6'),
  9. (5.0, 5, '5'),
  10. (4.0, 4, '4'),
  11. (3.0, 3, '3'),
  12. (2.0, 2, '2'),
  13. (1.0, 1, '1')]
  14. let y = x.reversed
  15. block:
  16. let res = collect:
  17. for (f, i, c) in x:
  18. (f, i, c)
  19. doAssert res == x
  20. iterator popAscending[T](q: var seq[T]): T =
  21. while q.len > 0: yield q.pop
  22. block:
  23. var res = collect:
  24. for f, i, c in popAscending(x):
  25. (f, i, c)
  26. doAssert res == y
  27. let z = reversed(res)
  28. let res2 = collect:
  29. for (f, i, c) in popAscending(res):
  30. (f, i, c)
  31. doAssert res2 == z
  32. block:
  33. var visits = 0
  34. block:
  35. proc bar(): (int, int) =
  36. inc visits
  37. (visits, visits)
  38. iterator foo(): (int, int) =
  39. yield bar()
  40. for a, b in foo():
  41. doAssert a == b
  42. doAssert visits == 1
  43. block:
  44. proc iterAux(a: seq[int], i: var int): (int, string) =
  45. result = (a[i], $a[i])
  46. inc i
  47. iterator pairs(a: seq[int]): (int, string) =
  48. var i = 0
  49. while i < a.len:
  50. yield iterAux(a, i)
  51. var x = newSeq[int](10)
  52. for i in 0 ..< x.len:
  53. x[i] = i
  54. let res = collect:
  55. for k, v in x:
  56. (k, v)
  57. let expected = collect:
  58. for i in 0 ..< x.len:
  59. (i, $i)
  60. doAssert res == expected
  61. block:
  62. proc bar(): (int, int, int) =
  63. inc visits
  64. (visits, visits, visits)
  65. iterator foo(): (int, int, int) =
  66. yield bar()
  67. for a, b, c in foo():
  68. doAssert a == b
  69. doAssert visits == 2
  70. block:
  71. proc bar(): int =
  72. inc visits
  73. visits
  74. proc car(): int =
  75. inc visits
  76. visits
  77. iterator foo(): (int, int) =
  78. yield (bar(), car())
  79. yield (bar(), car())
  80. for a, b in foo():
  81. doAssert b == a + 1
  82. doAssert visits == 6
  83. block:
  84. proc bar(): (int, int) =
  85. inc visits
  86. (visits, visits)
  87. proc t2(): int = 99
  88. iterator foo(): (int, int) =
  89. yield (12, t2())
  90. yield bar()
  91. let res = collect:
  92. for (a, b) in foo():
  93. (a, b)
  94. doAssert res == @[(12, 99), (7, 7)]
  95. doAssert visits == 7
  96. block:
  97. proc bar(): (int, int) =
  98. inc visits
  99. (visits, visits)
  100. proc t2(): int = 99
  101. iterator foo(): (int, int) =
  102. yield ((12, t2()))
  103. yield (bar())
  104. let res = collect:
  105. for (a, b) in foo():
  106. (a, b)
  107. doAssert res == @[(12, 99), (8, 8)]
  108. doAssert visits == 8
  109. block:
  110. proc bar(): (int, int) =
  111. inc visits
  112. (visits, visits)
  113. proc t1(): int = 99
  114. proc t2(): int = 99
  115. iterator foo(): (int, int) =
  116. yield (t1(), t2())
  117. yield bar()
  118. let res = collect:
  119. for a, b in foo():
  120. (a, b)
  121. doAssert res == @[(99, 99), (9, 9)]
  122. doAssert visits == 9
  123. block:
  124. proc bar(): ((int, int), string) =
  125. inc visits
  126. ((visits, visits), $visits)
  127. proc t2(): int = 99
  128. iterator foo(): ((int, int), string) =
  129. yield ((1, 2), $t2())
  130. yield bar()
  131. let res = collect:
  132. for a, b in foo():
  133. (a, b)
  134. doAssert res == @[((1, 2), "99"), ((10, 10), "10")]
  135. doAssert visits == 10
  136. block:
  137. proc bar(): (int, int) =
  138. inc visits
  139. (visits, visits)
  140. iterator foo(): (int, int) =
  141. yield (for i in 0 ..< 10: discard bar(); bar())
  142. yield (bar())
  143. let res = collect:
  144. for (a, b) in foo():
  145. (a, b)
  146. doAssert res == @[(21, 21), (22, 22)]
  147. block:
  148. proc bar(): (int, int) =
  149. inc visits
  150. (visits, visits)
  151. proc t2(): int = 99
  152. iterator foo(): (int, int) =
  153. yield if true: bar() else: (t2(), t2())
  154. yield (bar())
  155. let res = collect:
  156. for a, b in foo():
  157. (a, b)
  158. doAssert res == @[(23, 23), (24, 24)]
  159. block:
  160. iterator foo(): (int, int, int) =
  161. var time = 777
  162. yield (1, time, 3)
  163. let res = collect:
  164. for a, b, c in foo():
  165. (a, b, c)
  166. doAssert res == @[(1, 777, 3)]
  167. block:
  168. iterator foo(): (int, int, int) =
  169. var time = 777
  170. yield (1, time, 3)
  171. let res = collect:
  172. for t in foo():
  173. (t[0], t[1], t[2])
  174. doAssert res == @[(1, 777, 3)]
  175. block:
  176. proc bar(): (int, int, int) =
  177. (1, 2, 3)
  178. iterator foo(): (int, int, int) =
  179. yield bar()
  180. let res = collect:
  181. for a, b, c in foo():
  182. (a, b, c)
  183. doAssert res == @[(1, 2, 3)]