titer.nim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. discard """
  2. output: '''
  3. testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest2!test3?hi
  4. what's
  5. your
  6. name
  7. hi
  8. what's
  9. your
  10. name
  11. '''
  12. """
  13. # Test the new iterators
  14. iterator xrange(fromm, to: int, step = 1): int =
  15. var a = fromm
  16. while a <= to:
  17. yield a
  18. inc(a, step)
  19. iterator interval[T](a, b: T): T =
  20. var x = a
  21. while x <= b:
  22. yield x
  23. inc(x)
  24. #
  25. #iterator lines(filename: string): (line: string) =
  26. # var
  27. # f: tTextfile
  28. # shouldClose = open(f, filename)
  29. # if shouldClose:
  30. # setSpace(line, 256)
  31. # while readTextLine(f, line):
  32. # yield line
  33. # finally:
  34. # if shouldClose: close(f)
  35. #
  36. for i in xrange(0, 5):
  37. for k in xrange(1, 7):
  38. write(stdout, "test")
  39. for j in interval(45, 45):
  40. write(stdout, "test2!")
  41. write(stdout, "test3?")
  42. for x in items(["hi", "what's", "your", "name"]):
  43. echo(x)
  44. const
  45. stringArray = ["hi", "what's", "your", "name"]
  46. for i in 0..len(stringArray)-1:
  47. echo(stringArray[i])
  48. # bug #15360
  49. type Rule[T] = (int, T)
  50. var t: seq[Rule[int]]
  51. for (c, t) in t:
  52. discard
  53. import std/sugar
  54. # bug #14165
  55. iterator log_nodups_hamming(): int {.inline.} =
  56. let lb3 = 1
  57. let lb4 = 123
  58. proc mul3(): int = lb3 + lb4
  59. yield mul3()
  60. for h in log_nodups_hamming():
  61. break
  62. for h in log_nodups_hamming():
  63. break
  64. for h in log_nodups_hamming():
  65. break
  66. # bug #18536
  67. iterator envPairs*(): int =
  68. var foo: seq[int]
  69. proc fun() =
  70. foo = @[]
  71. fun()
  72. yield 3
  73. proc main() =
  74. for a in envPairs():
  75. discard
  76. for a in envPairs():
  77. discard
  78. static: main()
  79. main()
  80. # bug #6269
  81. iterator makeFn(outer_val: int): proc(a: int): int =
  82. for i in 0..1:
  83. yield proc(a:int): int =
  84. return a + i.int
  85. let v1 = 42
  86. let res = collect:
  87. for fn1 in makeFn(v1):
  88. let v2 = fn1(v1)
  89. for fn2 in makeFn(v2):
  90. fn2(v2)
  91. doAssert res == @[42, 43, 43, 44]