<div dir="ltr"><div>It's a fundamental mathematical operator (it's actually the 'true modulo' operation), the % operator is the remainder operation. This distinction is not a made up distinction (e.g. <a href="https://blogs.msdn.microsoft.com/ericlippert/2011/12/05/whats-the-difference-remainder-vs-modulus/">https://blogs.msdn.microsoft.com/ericlippert/2011/12/05/whats-the-difference-remainder-vs-modulus/</a>)</div><div><br></div><div><span style="font-size:12.8px">> You can simply use arr.endIndex.advancedBy(-1) for</span><br style="font-size:12.8px"></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">You are not taking into account when a function is passed an argument and needs it to be in some range. It's not that it's impossible to implement, it's just that this is more ergonomic.</span></div><div><span style="font-size:12.8px"><br></span></div><div><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 21, 2016 at 2:09 PM, Leonardo Pessoa <span dir="ltr"><<a href="mailto:me@lmpessoa.com" target="_blank">me@lmpessoa.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Adam, there is no need for an operator to compute the real index using<br>
a negative index. You can simply use arr.endIndex.advancedBy(-1) for<br>
that or, in this particular case, arr.endIndex.predecessor(). Right<br>
now this is the standard, although I''d rather use int indices<br>
directly.<br>
<span class="im HOEnZb"><br>
On 21 May 2016 at 17:56, Adam Nemecek <<a href="mailto:adamnemecek@gmail.com">adamnemecek@gmail.com</a>> wrote:<br>
>> I think this is a very particular case<br>
><br>
> I disagree. Swift has the concept of a range all over the place and this is<br>
> a fundamental range operation.<br>
><br>
>> As you pointed out this already can be implemented using resources<br>
>> already available in the language when needed.<br>
><br>
> Right. A lot of things in the standard library can be implemented using<br>
> resources already available, it's about the ergonomics.<br>
><br>
><br>
><br>
> On Sat, May 21, 2016 at 1:44 PM, Leonardo Pessoa <<a href="mailto:me@lmpessoa.com">me@lmpessoa.com</a>> wrote:<br>
>><br>
>> Adam, I think this is a very particular case and not something that needs<br>
>> to be added to the language. As you pointed out this already can be<br>
>> implemented using resources already available in the language when needed.<br>
>><br>
>><br>
>><br>
</span><div class="HOEnZb"><div class="h5">>> On 21 May 2016 at 17:22, Adam Nemecek via swift-evolution<br>
>> <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>>><br>
>>> Hello,<br>
>>><br>
>>> I think that Swift could use the 'double modulo' operator which is for<br>
>>> example in CoffeeScript (some discussion can be found here<br>
>>> <a href="https://github.com/jashkenas/coffeescript/issues/1971" rel="noreferrer" target="_blank">https://github.com/jashkenas/coffeescript/issues/1971</a>).<br>
>>><br>
>>> This operator, unlike normal modulo, takes sign from the divisor, not the<br>
>>> dividend e.g. -10 % 3 == -1, but -10 %% 3 == 2.<br>
>>><br>
>>> In practice, this operator is useful for 'cyclical' indexing. For<br>
>>> example, it would be useful for calculating the real index into a collection<br>
>>> when we are using an index outside of the range of valid indices and could<br>
>>> be used to index into a collection using a negative index à la Python and<br>
>>> Ruby (where [1,2,3,4][-1] == 4).<br>
>>><br>
>>><br>
>>> The implementation would probably be something along these lines:<br>
>>><br>
>>> infix operator %% {<br>
>>> associativity left<br>
>>> precedence 150<br>
>>> }<br>
>>><br>
>>> func %%<T: IntegerArithmeticType>(lhs:T, rhs:T) -> T {<br>
>>> return (lhs % rhs + rhs) % rhs<br>
>>> }<br>
>>><br>
>>> If accepted, this could be later incorporated into a method or operator<br>
>>> that works directly with collections using their count property.<br>
>>> Maybe the syntax could be something like [1,2,3,4] %% -1 == 4.<br>
>>><br>
>>> Ideas, suggestions?<br>
>>><br>
>>> _______________________________________________<br>
>>> swift-evolution mailing list<br>
>>> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
>>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
>>><br>
>><br>
><br>
</div></div></blockquote></div><br></div>