5 ms·
Arrays start at 1. Just... no.
by taylorius 3y ago
Arrays start at 1. Just... no.
- josephcsible 3y agoYeah that's a wart, but it's basically the only wart in an otherwise amazing language.
- fbdab103 3y agoEhh, local being a keyword (instead of local default, global only if required) was a significantly bigger gaff. The other would be no nulls in tables. 0/1 based indexing is familiarity only.
- josephcsible 3y agoRequiring a keyword to declare a variable is a good thing. Otherwise, it'd be easily to accidentally declare new variables when you meant to assign to an existing one. Since global is the default, starting a program with something like this effectively keeps that from happening: local gindex = {} setmetatable(_G, {__index = function(t,k) local v = gindex[k] if v == nil then error("Bad read of global variable " .. tostring(k), 2) else return v end end, __newindex = function(t,k,v) error("Bad write of global variable " .. tostring(k), 2) end}) for k,v in pairs(_G) do gindex[k] = v rawset(_G, k, nil) end
- ok_dad 3y agoWhy? 0 based indexing came from a time where you used memory addresses directly to index into arrays. Today, we don’t do that, so 1 based indexing seems extremely reasonable. Besides, with Lua you can still do 0 based indexing with a small personal extension function.
- imbnwa 3y agoOne of the first things requested by users of an internal media sequencing tool where time is measured by frames was 1-based indexing, they absolutely hated starting from 0
- ok_dad 3y agoYea, I love how 0 indexing is such a big deal. Like tabs vs spaces, it doesn’t matter even a tiny bit, it’s like a religion. It’s not that hard to index off of 1, and like I said, you can write like 5 lines of code to make 0 indexing the default!
- krylon 3y agoFrom an aesthetic perspective, I agree. In practice, it wasn't a problem, though. I rarely accessed arrays with a constant index anyway.
- bryanrasmussen 3y agomaybe I'm being dim but what possible aesthetic benefit does it offer? The only benefit that springs to my mind from a language design perspective is that it is most likely familiar to the user of the language from languages they have used before. Both Smalltalk and APL use 1 based indices so I just find any claim of obvious aesthetic superiority on this issue a little hard to accept without some back up. If speaking aesthetics the fact that the number of items in your array is equal to the position of the last index item in the array would seem aesthetically superior.
- ufo 3y agoLua's 1-based indexing is largely inspired by Fortran. Lua's first users were Brazilian engineers in the early 90s, who were mostly experienced in Fortran. Arguably, one-based indexing is more closed aligned to mathematics tradition. That point about being easier to index the last element helps to iterate backwards over an array. With zero-based indexing, a backwards loop becomes for(i=N-1; i>=0; i--). Yuck.
- bryanrasmussen 3y agoexactly, I have a hard time understanding any argument for 0 based indexing other than tradition or a performance hack that I find unlikely was ever that great a benefit anyway.
- taylorius 3y agoIt is admittedly a mindset that is born of C coding. Pointers and memory offsets and so forth. But lots of languages that abstract such "close to the metal" factors away, still use 0 based indexing.
- 3y ago
- huimang 3y agoVery insightful discussion.
- taylorius 3y agoThank you. I'm here all week.. :-)
- ufo 3y agoI like the arguement that 0-based indexing is better for representing offsets, but 1-based indexing is better for representing actual indices. https://hisham.hm/2021/01/18/again-on-0-based-vs-1-based-indexing/ https://hisham.hm/2021/01/18/again-on-0-based-vs-1-based-ind...
- rdtsc 3y agoNobody starts counting things at 0 in real life so… yes please!
- taylorius 3y agoI suppose it's whether you think of array indexing as an operation that offsets the read location relative to th start of the array. It's probably rather low level for the way people are used to working these days.
- rdtsc 3y agoI think it's just common sense of pointing to the first location and saying "one" ("first element"), "two" ("second element") and so on.