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 ]

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)

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 04, 14:41