logoalt Hacker News

LoganDark • yesterday at 2:44 PM • 3 replies • view on HN

Lua's 1-based inclusive indexing makes math so annoying. Plus- and minus-one everywhere.


Replies

ufo • yesterday at 7:33 PM

I find that in practice the amount of +-1 is about the same for zero-based and one-based indexing. Each has situations it is better suited for. For example, iterating a list backwards is nicer with one-based indexes.

➕ show 1 reply
otikik • yesterday at 5:21 PM

That is absolutely not the case.

Numerical loops in Lua are inclusive:

    for i=1,#t do t[i]=0 end
On the example above the i starts at 1 and goes up to (and including) the length of t.

With 0-based indexes, and that numerical loop format, it would be minus-one everywhere:

    for i=1,#t-1 do ...
➕ show 1 reply
ModernMech • yesterday at 3:08 PM

These days all you gotta do is ask your local LLM to write you a Lua with 0-based indexing. Sure beats complaining about 1-based indexing on HN until the end of time.

➕ show 1 reply