titer13.nim 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. discard """
  2. output: '''b yields
  3. c yields
  4. a returns
  5. b yields
  6. b returns
  7. c yields
  8. 1
  9. 2
  10. 3
  11. 4
  12. '''
  13. """
  14. block:
  15. template tloop(iter: untyped) =
  16. for i in iter():
  17. echo i
  18. template twhile(iter: untyped) =
  19. let it = iter
  20. while not finished(it):
  21. echo it()
  22. iterator a(): auto {.closure.} =
  23. if true: return "a returns"
  24. yield "a yields"
  25. iterator b(): auto {.closure.} =
  26. yield "b yields"
  27. if true: return "b returns"
  28. iterator c(): auto {.closure.} =
  29. yield "c yields"
  30. if true: return
  31. iterator d(): auto {.closure.} =
  32. if true: return
  33. yield "d yields"
  34. tloop(a)
  35. tloop(b)
  36. tloop(c)
  37. tloop(d)
  38. twhile(a)
  39. twhile(b)
  40. twhile(c)
  41. twhile(d)
  42. block:
  43. iterator a: auto =
  44. yield 1
  45. for x in a():
  46. echo x
  47. let b = iterator: int =
  48. yield 2
  49. for x in b():
  50. echo x
  51. let c = iterator: auto =
  52. yield 3
  53. for x in c():
  54. echo x
  55. block:
  56. iterator myIter2(): auto {.closure.} =
  57. yield 4
  58. for a in myIter2():
  59. echo a
  60. block t5859:
  61. proc flatIterator[T](s: openarray[T]): auto {.noSideEffect.}=
  62. result = iterator(): auto =
  63. when (T is not seq|array):
  64. for item in s:
  65. yield item
  66. else:
  67. yield 123456
  68. # issue #5859
  69. let it = flatIterator(@[@[1,2], @[3,4]])