tyield.nim 4.3 KB

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