|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
iteratorsHere 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.
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 In Nim, an iterator —by default— is an inline iterator. An inline iterator can only be used in 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. |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |
||||