News for hackers. Stuff that matters.
Re:Teaching the Unfortunate Parts by Spartan-S63 on Monday December 28, 2020 @07:42PM (#25565233)
If your mental model is that an array is zero-indexed but circular, I think arr[-1] still is consistent with that model. If you don’t think of arrays as circular, then sure, it doesn’t make sense.

I’m still in agreement that negative indices make it more confusing than just calculating based off the length, but I get why they exist.

Re:Teaching the Unfortunate Parts by ironmagma on Monday December 28, 2020 @08:29PM (#25565588)
The mental model doesn’t really jive with circularity since arr[arr.length] is not the same as arr[0] in any language that I know of. With a circle, you would expect that arr[0], arr[length], and arr[2*length] are all identical.
Re:Teaching the Unfortunate Parts by jniedrauer on Monday December 28, 2020 @07:59PM (#25565356)
You can also think of negative indexes as a shorthand for length-N. arr[-1] is the same as arr[len(arr)-1].