The sequence also contains a collection of elements of the same data type similar to arrays.

The Sequence contains a fixed size length. The sequence contains a dynamic size length.

Important points about the Nim sequence

  • sequence contains a collection of elements of the same data type under a single variable name
  • Dynamic size
  • Sequence index always starts with 0 and ends with length -1
  • Index can also contain not only numbers but other ordinal types such as chars, strings
  • It allocates memory on the heap

Let’s see different examples.

How to declare Sequence in Nim

The sequence can be declared in three ways.

  • empty sequence
  • nonempty values
  • Sequence inline assignment Sequence declaration syntax
\## empty sequence
var variable: seq[datatype]([size])
\## Nonempty sequence
var variable: newSeq[datatype]([size])
\## sequence inline declaration
var variable= @[values separated by comma]

Size is the number of elements in size and optional Datatype is a NIM predefined type. the seq and newSeq are keywords in the NIM variable is a valid identifier

Examples of declaring a Sequence of different ways

empty sequence: A sequence of integers is declared with a dynamic size and elements in the sequence are empty.

var numbers: seq[int]
echo numbers

Output:

@[]

non-empty values sequence: A Sequence of floats is declared with a fixed size and elements in the Sequence are initialized with default float zero.

var numbers: newSeq[float](10)
echo numbers

Output:

@[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

sequence inline assignment syntax:

The sequence can also be declared without datatype and assigned with values using @[] syntax. Elements are separated by a comma.

The data type is inferred from the type of the element

var numbers:  @[1,2,3,4]
echo numbers

Output:

@[1,2,3,4]

How to access the sequence elements in a NIM Sequence?

sequence elements are accessed using an indexing mechanism.

The first element in the sequence starts with zero and the last element is length -1.

var numbers = @[1,2,3,4]
echo numbers[0] ## 1
echo numbers[3] ## 4

Another way to get Start and End indexes using low and high procs in sequence.

var numbers = @[1,2,3,4]
echo numbers.low ##0
echo numbers.high ##3

It throws Error: unhandled exception: index 4 not in 0 .. 3 [IndexDefect]

Length of a sequence in NIM

len proc returns the number of elements in a sequence.

var numbers = @[1,2,3]
echo numbers.len

How to add and delete elements into a sequence in NIm

sequence provides the below procs to add an element into syntax.

  • assignment syntax
  • add
  • insert

assignment syntax:

Elements add at the index position and the Sequence is restructured using the below syntax.

sequence[index]=value

For example, to add a Sequence at the index position, use sequence[0]=1

Here is an example

var numbers = @[1,2,3,4]
echo numbers
numbers[1]=11
echo numbers

Output:

@[1, 2, 3, 4]
@[1, 11, 3, 4]

add proc:

elements added to the end of the sequence using add proc.

var numbers = @[1,2,3,4]
echo numbers
numbers.add(11)
echo numbers

Output:

@[1, 2, 3, 4]
@[1, 2, 3, 4,11]

insert proc:

elements added to the sequence using an index with insert proc.

var numbers = @[1,2,3,4]
echo numbers
numbers.insert(11,1)
echo numbers

Output:

@[1, 2, 3, 4]
@[1, 11, 3, 4]

Sequence elements can be deleted using the delete proc.

var numbers = @[1,2,3,4]
echo numbers
numbers.delete(1)
echo numbers

Output:

@[1, 2, 3, 4]
@[1, 3, 4]

Nim Multi-dimensional sequence

Multi-dimensional sequences are sequences of sub-sequences.

Example multi-dimensional are two-dimensional sequences i.e matrix

import sequtils

var matrix = newSeqWith(2, newSeq[int](2))
echo matrix
matrix[0][0] = 1
matrix[1][0] = 2
matrix[0][1] = 3
matrix[1][1] = 4

echo matrix

Output:

@[@[0, 0], @[0, 0]]
@[@[1, 3], @[2, 4]]

Iterate a sequence with an example

sequence elements iterated using for in-loop syntax.

The below example iterates a sequence using value.

var sequnce = @[1,2,3]
for value in sequnce:
  echo value

Output:

1
2
3

For loop contains an object that contains the index and value for each iteration.

var sequnce = @[1,2,3]
for index, value in sequnce:
  echo index,"=", value

Output:

0=1
1=2
2=3