<div dir="ltr">Would you want to make this a function? Or an operator but a different one?</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 23, 2016 at 7:30 AM, Stephen Canon <span dir="ltr"><<a href="mailto:scanon@apple.com" target="_blank">scanon@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I’m not really sold on the `%%` spelling, but I think the operation itself is worth exposing. This is the remainder of a “flooring” division (as opposed to the C-family “truncating” division[1]). If we do provide it, we should also provide the accompanying divide operation.</div><div><br></div><div>– Steve</div><div><br></div><div>[1] there are several other ways to define division beyond these two: remainder is always positive, remainder is closest to zero, etc. Truncating and flooring division are the most common by a wide margin, however.</div><div><br><div><blockquote type="cite"><div><div class="h5"><div>On May 21, 2016, at 4:22 PM, Adam Nemecek via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br></div></div><div><div><div class="h5"><div dir="ltr"><div>Hello, </div><div><br></div><div>I think that Swift could use the 'double modulo' operator which is for example in CoffeeScript (some discussion can be found here <a href="https://github.com/jashkenas/coffeescript/issues/1971" target="_blank">https://github.com/jashkenas/coffeescript/issues/1971</a>).</div><div><br></div><div>This operator, unlike normal modulo, takes sign from the divisor, not the dividend e.g. -10 % 3 == -1, but -10 %% 3 == 2.</div><div><br></div><div>In practice, this operator is useful for 'cyclical' indexing. For example, it would be useful for calculating the real index into a collection when we are using an index outside of the range of valid indices and could be used to index into a collection using a negative index à la Python and Ruby (where [1,2,3,4][-1] == 4).</div><div><br></div><div><br></div><div>The implementation would probably be something along these lines:</div><div><br></div><div>infix operator %% {</div><div> associativity left</div><div> precedence 150</div><div>}</div><div><br></div><div>func %%<T: IntegerArithmeticType>(lhs:T, rhs:T) -> T {</div><div> return (lhs % rhs + rhs) % rhs</div><div>}</div><div><br></div><div>If accepted, this could be later incorporated into a method or operator that works directly with collections using their count property. </div><div>Maybe the syntax could be something like [1,2,3,4] %% -1 == 4.</div><div><br></div><div>Ideas, suggestions?</div></div></div></div><span class="">
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></blockquote></div><br></div></div></blockquote></div><br></div>