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 ]

Sequences

A sequence is a dynamic array. Same thing as a list in Python. Sequences are stored on the heap, thus they are garbage collected.

basic usage
def main():
    li = [1, 8, 5]
    print(li)  # [1, 8, 5]

    numbers = []

    numbers.append(2)
    numbers.append(3)
    numbers.append(5)

    print(numbers)  # [2, 3, 5]

    for n in numbers:
        print(n)

    print(len(numbers))  # 3
proc main() =
  var li = @[1, 8, 5]
  echo li  # @[1, 8, 5]

  var numbers1: seq[int]
  var numbers2: seq[int] = @[]
  var numbers3 = newSeq[int]()

  echo numbers1  # @[]
  echo numbers2  # @[]
  echo numbers3  # @[]

  numbers1.add(2)
  numbers1.add(3)
  numbers1.add(5)

  echo numbers1  # @[2, 3, 5]

  for n in numbers1:
    echo(n)

  echo numbers1.len  # 3

When you simply write [1, 2, 3], it's a static array. When you apply the '@' operator: @[1, 2, 3], it'll transform the static array to a dynamic array.

In the Nim code, numbers1, numbers2 and numbers3 are equivalent. They are all empty sequences.

var nums = @[]  # ERROR: cannot infer the type

𝥶It doesn't work because the compiler cannot figure out the type of the sequence.

var nums = @[1, 2, 3]  # OK

𝥶It's OK, nums will be a sequence of integers.

var numbers1: seq[int]

𝥶Here we declare a sequence of integers. Nim will initialize it, thus it becomes an empty sequence.

echo numbers1.len

𝥶This if UFCS. It's actually transformed to len(numbers1)

Set the capacity

If you know a priori how many elements you want to store in a seq, then you can set its capacity to avoid reallocations.

var li = newSeqOfCap[int](10_000)

echo len(li)        # 0

li.add(5)
li.add(8)

echo len(li)        # 2

Now, in this list you can insert 10,000 elements and you can be sure that no reallocations will happen. When you want to insert the 10,001st element, then the list will grow itself to make room for the new element. A common dynamic array growth strategy is to double the capacity, so at the 10,001st element the capacity would jump to ~20,000.

Set the length

If you know a priori how many elements you want to store in a seq, then you can set its length to avoid reallocations.

However! This time the sequence will be filled with zeroed entries! After the creation of the sequence, you should assign entries to the sequence (using the indexes) instead of adding them!

var li = newSeq[int](10)

echo li             # @[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

echo len(li)        # 10

li[0] = 1
li[1] = 2
echo li             # @[1, 2, 0, 0, 0, 0, 0, 0, 0, 0]

li.add(99)
echo li             # @[1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 99]
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 01, 23:55