Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Helps how?


Wrapping around the array, you just need for your position to be i%len

Though ok, reading an array backwards requires a len-1-i


Why 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.


I 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


Ringbuffers are used all over


Or alternatively, len + ~i

Okay, maybe don't use that one in production :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: