tyield.nim 4.3 KB

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