[swift-evolution] [Pitch] 'Double modulo' operator

Adam Nemecek adamnemecek at gmail.com
Sat May 21 16:30:19 CDT 2016


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.
https://blogs.msdn.microsoft.com/ericlippert/2011/12/05/whats-the-difference-remainder-vs-modulus/
)

> You can simply use arr.endIndex.advancedBy(-1) for

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.





On Sat, May 21, 2016 at 2:09 PM, Leonardo Pessoa <me at lmpessoa.com> wrote:

> Adam, there is no need for an operator to compute the real index using
> a negative index. You can simply use arr.endIndex.advancedBy(-1) for
> that or, in this particular case, arr.endIndex.predecessor(). Right
> now this is the standard, although I''d rather use int indices
> directly.
>
> On 21 May 2016 at 17:56, Adam Nemecek <adamnemecek at gmail.com> wrote:
> >> I think this is a very particular case
> >
> > I disagree. Swift has the concept of a range all over the place and this
> is
> > a fundamental range operation.
> >
> >>  As you pointed out this already can be implemented using resources
> >> already available in the language when needed.
> >
> > Right. A lot of things in the standard library can be implemented using
> > resources already available, it's about the ergonomics.
> >
> >
> >
> > On Sat, May 21, 2016 at 1:44 PM, Leonardo Pessoa <me at lmpessoa.com>
> wrote:
> >>
> >> Adam, I think this is a very particular case and not something that
> needs
> >> to be added to the language. As you pointed out this already can be
> >> implemented using resources already available in the language when
> needed.
> >>
> >>
> >>
> >> On 21 May 2016 at 17:22, Adam Nemecek via swift-evolution
> >> <swift-evolution at swift.org> wrote:
> >>>
> >>> Hello,
> >>>
> >>> I think that Swift could use the 'double modulo' operator which is for
> >>> example in CoffeeScript (some discussion can be found here
> >>> https://github.com/jashkenas/coffeescript/issues/1971).
> >>>
> >>> This operator, unlike normal modulo, takes sign from the divisor, not
> the
> >>> dividend e.g. -10 % 3 == -1, but -10 %% 3 == 2.
> >>>
> >>> 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).
> >>>
> >>>
> >>> The implementation would probably be something along these lines:
> >>>
> >>> infix operator %% {
> >>>   associativity left
> >>>   precedence 150
> >>> }
> >>>
> >>> func %%<T: IntegerArithmeticType>(lhs:T, rhs:T) -> T {
> >>>   return (lhs % rhs + rhs) % rhs
> >>> }
> >>>
> >>> If accepted, this could be later incorporated into a method or operator
> >>> that works directly with collections using their count property.
> >>> Maybe the syntax could be something like [1,2,3,4] %% -1 == 4.
> >>>
> >>> Ideas, suggestions?
> >>>
> >>> _______________________________________________
> >>> swift-evolution mailing list
> >>> swift-evolution at swift.org
> >>> https://lists.swift.org/mailman/listinfo/swift-evolution
> >>>
> >>
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160521/38157152/attachment.html>


More information about the swift-evolution mailing list