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
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

items(), pairs()

These are read-only iterators.

mitems(), mpairs(): mutable iterators (see here)

items()

Iterate over the elements.

let li = @["aa", "bb", "cc"]

for x in li:
  echo x

echo "---"

for x in li.items():
  echo x

Output:

aa
bb
cc
---
aa
bb
cc

When you iterate over something with a for loop, Nim will implicitly call the iterator items(). This iterator is invoked when you specify just one variable in the for loop.

pairs()

pairs() is similar to items(), but in addition, you also get the index.

let li = @["aa", "bb", "cc"]

for idx, x in li:
  echo idx, ": ", x

echo "---"

for idx, x in li.pairs():
  echo idx, ": ", x

Output:

0: aa
1: bb
2: cc
---
0: aa
1: bb
2: cc

When you specify two variables in the for loop, then the pairs() iterator is invoked. The iterator returns the index of the current element, and the current element itself.

Read-only

In the body of the for loop, the value of the loop variable cannot be modified.

var li = @[1, 2, 3]   # `var` is used -> mutable

for x in li:
  x = 9               # Error: 'x' cannot be assigned to
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 May 06, 20:24