3 ms·
Well yes, Python or js don't have pointer arithmetic but underneath that's what happens if you're dealing with an array Zero indexing might look weird at first
by raverbashing 6y ago
Well yes, Python or js don't have pointer arithmetic but underneath that's what happens if you're dealing with an array
Zero indexing might look weird at first but I think it helps in most cases.
- IlliOnato 6y agoHelps how?
- raverbashing 6y agoWrapping around the array, you just need for your position to be i%len Though ok, reading an array backwards requires a len-1-i
- IlliOnato 6y agoWhy would you need to "wrap around" an array? I guess it's helpful if you use an array to represent a cyclic group, but how often this happens in practice? And when you need the number of the last element, you need to do A[n-1], which is "less helpful", I guess.
- raverbashing 6y agoI think both those cases happen with a certain frequency, yes, your last element is a bit trickier, though in Python it is represented by A[-1] (so that would turn into A[len-1]) Yeah, maybe there's no fundamental advantage, one would have to check the most common cases. > Why would you need to "wrap around" an array? Ring buffers
- secondcoming 6y agoRingbuffers are used all over
- hmry 6y agoOr alternatively, len + ~i Okay, maybe don't use that one in production :)
- jakelazaroff 6y agoIsn't the whole point of using a language like Python or JS to make it so you don't have to worry about what happens "underneath"?
- raverbashing 6y agoUp to a point yes, but abstractions only go so far Though yes, adding or subtracting one is a minor thing
- dgellow 6y agoYou still have to understand what is being abstracted if you want to do anything useful. You cannot just ignore what data structures are and how they work or you will have some very bad surprises. Also often abstractions (and their quirkiness) make way more sense when you understand what is being abstracted.
- andreareina 6y agoIf the language doesn't expose pointers, then that's an abstraction leak. Even if it does, why must that dictate the syntax and semantics? I'm sure our compilers and interpreters are capable of turning arr[i] into *arr + (i + 1)