3 ms·
I find true module to be an insanely useful operator. Python negative indexing (array[-1]) is essentially that. I’ve proposed it to swift but people didn’t see
by adamnemecek 8y ago
I find true module to be an insanely useful operator. Python negative indexing (array[-1]) is essentially that. I’ve proposed it to swift but people didn’t see the point https://forums.swift.org/t/double-modulo-operator/2718 https://forums.swift.org/t/double-modulo-operator/2718
- tlb 8y agoBut you want array[len(array)] to throw an error rather than return array[0], so modulo isn't the right operator.
- Shoop 8y agoPython negative indexing isn't modulo. It's just shorthand for array[len(array)-i]. >>> a = [1, 2, 3] >>> a[-1] 3 >>> a[-2] 2 >>> a[-3] 1 >>> a[-4] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
- adamnemecek 8y agoThe problem is that you need to have a conditional to figure out if I is negative or not. With modulo it’s all the same.