Apologies if this has been asked before, I am new here.
I started learning Nim yesterday, and read through the [tutorial|https://nim-lang.org/docs/tut1.html], where it says:
> Zero-indexed counting have two shortcuts ..< and ..^ to simplify counting to one less than the higher index:
But compiling the following program with <, I get a compiler deprecation warnings:
import tables
import strutils
proc diff_by_one(w: string, w2: string): string =
var d: int = 0
var pos: int = 0
for i, ch in w.pairs:
if ch != w2[i]:
d += 1
pos = i
if d != 1:
return ""
let a = w[0 .. <pos]
let b = w[pos+1 .. <w.len]
return join(@[a, b], "")
proc solve(words: seq[string]): string =
for i, w in words.pairs:
for u in i .. <words.len:
var w2 = words[u]
var ans = diff_by_one(w, w2)
if ans != "":
return ans
return ""
var words: seq[string]
for line in "input".lines:
words.add(line)
echo solve(words)
These are the warnings:
part2.nim(13, 18) Warning: < is deprecated [Deprecated]
part2.nim(14, 22) Warning: < is deprecated [Deprecated]
part2.nim(19, 19) Warning: < is deprecated [Deprecated]
Should I replace usage of < with -1? If this feature is deprecated, should we update the tutorial?
It is now
for u in i ..< words.len:
So no space between .. and <. The old notation with space could give wrong results in rare cases due to operator priority.
Also note the conceptual difference:
for x in someIterable: foo(x) deals with the elements of someIterable, e.g. with the letters of a string.
for i in 0 ..> something.len: foo(i) is conceptionally quite different albeit well known from most older languages and deals with the indices.
More often than not the former is what is actually desired but the latter is used because it's what we grew up with in C.
Also note that Nim has the very valuable option of low and high for the "indexed loop variant" which frees you from worrying about and using .. vs. ..<.
For example for i in someArray.low .. someArray.high: foo(i) safely walks from the first element of someArray (whatever that may be) to the last element or more precisely yields all indices from the first to the last one.