summary refs log tree commit diff stats
path: root/tests/iter/titer3.nim
blob: 9dcfd7be5b0ea72ac88e51d9f4ba140449022752 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
discard """
  output: '''1231
4
6
8
--------
4
6
8
'''
"""

iterator count1_3: int =
  yield 1
  yield 2
  yield 3

for x in count1_3():
  write(stdout, $x)

# yield inside an iterator, but not in a loop:
iterator iter1(a: openArray[int]): int =
  yield a[0]

var x = [[1, 2, 3], [4, 5, 6]]
for y in iter1(x[0]): write(stdout, $y)
writeLine(stdout, "")

# ensure closure and inline iterators have the same behaviour wrt
# parameter passing

iterator clo(a: int): int {.closure.} =
  yield 0+a
  yield 1+a
  yield 2+a

iterator inl(a: int): int {.inline.} =
  yield 0+a
  yield 1+a
  yield 2+a

proc main =
  var y = 4
  for i in clo(y):
    echo i
    inc y

  echo "--------"
  y = 4
  for i in inl(y):
    echo i
    inc y

main()