Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

iterators

Here I'll only talk about inline iterators. In Nim, an iterator —by default— is an inline iterator.

There are also closure ietrators, but I won't cover them here.

iterators
def between(lo, hi):
    i = lo
    while i <= hi:
        yield i
        i += 1


for n in between(12, 16):
    print(n)

print("---")

for n in between(12, 16):
    print(n)
iterator between(lo, hi: int): int =
  var i = lo
  while i <= hi:
    yield i
    i += 1


for n in between(12, 16):
  echo n

echo "---"

for n in between(12, 16):
  echo n

The output is the same in both cases:

12
13
14
15
16
---
12
13
14
15
16

Nim: similar to procedures but they use the keyword iterator. They yield values one at a time instead of returning a single value and stop. When used in a for loop, the iterator yields one value per iteration.

In Nim, an iterator —by default— is an inline iterator. An inline iterator can only be used in for loops.

Inline means that the compiler copies the iterator's body directly into the for loop (inlines it). So the code effectively becomes:

# first for loop:
var i = 12
while i <= 16:
  echo i
  i += 1

echo "---"

# second for loop:
var j = 12
while j <= 16:
  echo j
  j += 1

This is why you can use the same iterator in a second loop. There is no iterator object that could get exhausted — the compiler inlines the iterator body into each loop separately, so each loop gets its own fresh copy with its own local variables.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 07, 23:13