src/pykot

This is the main module of the pykot library. It started as a port of some Python / Kotlin features.

GitHub link: https://github.com/jabbalaci/nimpykot
Author: Laszlo Szathmary (Jabba Laci), 2018, 2025

Procs

func last[T](a: seq[T]): T
Last element of a sequence.

Example:

let numbers = @[2, 7, 4, 9]
doAssert numbers.last == 9
func lastIndex[T](a: seq[T]): int
Return the index of the last element of a sequence.

Example:

let
  a = @[4, 8, 3, 9]
doAssert a.lastIndex == 3

Iterators

iterator pyRange(a, b: int; step: Positive = 1): int {....raises: [], tags: [],
    forbids: [].}

An iterator that goes from a (incl.) until b (excl.). step is optional and must be >= 1.

Mimics Python's range(). system.range exists, so it had to be renamed. Negative step is not supported. Use countdown in that case.

Example:

var
  res = newSeq[int]()
for i in pyRange(5, 8):
  res.add(i)
doAssert res == @[5, 6, 7]
iterator pyRange(b: int): int {....raises: [], tags: [], forbids: [].}

An iterator that goes from 0 (incl.) until b (excl.).

Mimics Python's range(). system.range exists, so it had to be renamed.

Example:

var
  res = newSeq[int]()
for i in pyRange(5):
  res.add(i)
doAssert res == @[0, 1, 2, 3, 4]
iterator until(a, b: int): int {....raises: [], tags: [], forbids: [].}

An iterator that goes from a (incl.) until b (excl.).

I first saw this construction in Kotlin.

Example:

var
  res = newSeq[int]()
for _ in 1.until(5):
  res.add(0)
doAssert res == @[0, 0, 0, 0]

Templates

template loop(times: static[Natural]; body: untyped): untyped
Execute the body in a loop N times, i.e. repeat the body N times.

Example:

var cnt = 0
loop(5):
  inc cnt
doAssert cnt == 5