4 ms·
Really like the idea of using lazy ranges instead of magic negative indices! I.e `xs[6..]` instead of `xs[6..-1]`.
by augustl 8y ago
Really like the idea of using lazy ranges instead of magic negative indices! I.e `xs[6..]` instead of `xs[6..-1]`.
- pulisse 8y agoNegative indices aren't magic. They follow the obvious wraparound (modular) pattern: Negative `i` indexes the element in array `a` located at `i % len(a)`. If they worked any other way it'd be madness.
- mmahemoff 8y agoTrue, they're not magic, it just looks ugly for the most common case of fetching to the end of the array. The new syntax is much nicer than -1.
- viraptor 8y agoI don't think you got that index calculation right. -1 in array of 5 elements is located at 5-1 == 4, not at 1%5 == 1.
- kvakil 8y agoIt depends on how you define the modulo operator [0]--there are definitions where (-1)%5 == 4 and definitions where (-1)%5 == -1. Presumably the GP is referring to the former case. [0]: https://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation https://en.wikipedia.org/wiki/Modulo_operation#Remainder_cal...
- pulisse 8y agoI don't follow your point. We're concerned with the value of -1 % 5, not 1 % 5. There's no definition of the modulus operator on which -1 % 5 = 1.
- viraptor 8y agoPhrasing issue, I think. I understood "Negative `i` indexes the element" as `i` itself being positive. Like you'd say "negative 1 indexes" (i is 1), not "negative -1 indexes" (i is -1)