<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">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 class=""><br class=""></div><div class="">– Steve</div><div class=""><br class=""></div><div class="">[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 class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 21, 2016, at 4:22 PM, Adam Nemecek via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Hello, </div><div class=""><br class=""></div><div class="">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" class="">https://github.com/jashkenas/coffeescript/issues/1971</a>).</div><div class=""><br class=""></div><div class="">This operator, unlike normal modulo, takes sign from the divisor, not the dividend e.g. -10 % 3 == -1, but -10 %% 3 == 2.</div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class=""><br class=""></div><div class="">The implementation would probably be something along these lines:</div><div class=""><br class=""></div><div class="">infix operator %% {</div><div class=""> associativity left</div><div class=""> precedence 150</div><div class="">}</div><div class=""><br class=""></div><div class="">func %%<T: IntegerArithmeticType>(lhs:T, rhs:T) -> T {</div><div class=""> return (lhs % rhs + rhs) % rhs</div><div class="">}</div><div class=""><br class=""></div><div class="">If accepted, this could be later incorporated into a method or operator that works directly with collections using their count property. </div><div class="">Maybe the syntax could be something like [1,2,3,4] %% -1 == 4.</div><div class=""><br class=""></div><div class="">Ideas, suggestions?</div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>