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 ]

Python-like safe slicing

Slicing in Nim is very similar to Python. However, they are not completely identical.

Similarities

(1) similarities
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
s = "0123456789"

print(numbers[0:2])  # [0, 1]
print(s[0:2])        # 01

print(numbers[7:])   # [7, 8, 9]
print(s[7:])         # 789

print(numbers[-2:])  # [8, 9]
print(s[-2:])        # 89
let
  numbers = @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  s = "0123456789"

echo numbers[0 ..< 2]          # @[0, 1]
echo s[0 ..< 2]                # 01

echo numbers[7 .. ^1]          # @[7, 8, 9]
echo s[7 .. ^1]                # 789

echo numbers[^2 .. ^1]         # @[8, 9]
echo s[^2 .. ^1]               # 89

Differences

(2) differences
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
s = "0123456789"

print(numbers[2:0])  # []
print(s[2:0])  # "" (empty string)

print(numbers[8:100])  # [8, 9]
print(s[8:100])  # "89"
let
  numbers = @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  s = "0123456789"

# if start index > end index => error
#echo numbers[2 ..< 0]          # error
#echo s[2 ..< 0]                # error

# if end index > length => error
#echo numbers[8 ..< 100]         # error
#echo s[8 ..< 100]               # error

Solution

We can have a Python-like behaviour with the introduction of a safety function.

(3) solution
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
s = "0123456789"

print(numbers[2:0])  # []
print(s[2:0])  # "" (empty string)

print(numbers[8:100])  # [8, 9]
print(s[8:100])  # "89"

# normal call
print(numbers[2:5])  # [2, 3, 4]
print(s[2:5])  # "234"
import std/strutils  # join


proc safeSlice[T](arr: openArray[T], lo, hi: int): auto =
  ## Python-like (safe) slicing.
  ## `lo` is included, `hi` is NOT included (like in Python)
  if lo >= arr.len or lo > hi:
    when T is char: return ""
    else: return newSeq[T]()
  # else:
  let stop = if hi <= arr.len: hi else: arr.len
  when T is char: return arr[lo ..< stop].join
  else: return arr[lo ..< stop]

let
  numbers = @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  s = "0123456789"

echo numbers.safeSlice(2, 0);    # []
echo s.safeSlice(2, 0);          # "" (empty string)

echo numbers.safeSlice(8, 100);  # [8, 9]
echo s.safeSlice(8, 100);        # "89"

# normal call
echo numbers.safeSlice(2, 5);    # [2, 3, 4]
echo s.safeSlice(2, 5);          # "234"
  • when is a compile-time branch
  • auto as the return type lets Nim infer the correct type per instantiation (string or seq[T])

Links

  • In D, it's the same. See here.
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 19, 13:33