# divisible-by operator

Checking for divisibility is very common:

21 % 3 == 0 // true

In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:

21 %== 3 // true

… which also allows me to overload it for sequences:

21 %== [7, 3] // true

(If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)

milos

This is the kind of thing that I'd let live in a library and consider importing into the standard if a lot of people use it.

Félix

···

Le 6 avr. 2016 à 09:13:41, Milos Rankovic via swift-evolution <swift-evolution@swift.org> a écrit :

Checking for divisibility is very common:

21 % 3 == 0 // true

In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:

21 %== 3 // true

… which also allows me to overload it for sequences:

21 %== [7, 3] // true

(If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)

milos
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

While modulo checks are common, I don't think that your proposed solution (%==) enhances readability or saves typing *to such extent* that it vastly improves over the existing art:

21 % 3 == 0 reads easily from left to right, is quick to type, is understood across many languages.

21 %== 3 saves a few spaces, is less immediately understandable (due to the visual overlap with `+=` and `-=`) and would be (as far as I'm aware of) unique to Swift.

I applaud the thinking and creativity but I would not support the proposal.

-- E

···

On Apr 6, 2016, at 10:13 AM, Milos Rankovic via swift-evolution <swift-evolution@swift.org> wrote:

Checking for divisibility is very common:

21 % 3 == 0 // true

In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:

21 %== 3 // true

… which also allows me to overload it for sequences:

21 %== [7, 3] // true

(If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)

Personally I’d prefer to use a method like:

21.isDivisibleBy(3)

As it’s clearer IMO. My problem with the operator is that it doesn’t make logical sense to have the value you’re dividing by on the right hand side when there are equals signs, it seems more like this should be the value you’re testing. Perhaps there’s a better choice of operator such as:

21 %: 3

Though that’s probably not very clear either as the colon could be easy to miss. Basically the choice of %== doesn’t suggest its purpose, which makes it one more thing to learn, which is why a method may be a better choice.

···

On 6 Apr 2016, at 17:13, Milos Rankovic via swift-evolution <swift-evolution@swift.org> wrote:

Checking for divisibility is very common:

21 % 3 == 0 // true

In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:

21 %== 3 // true

… which also allows me to overload it for sequences:

21 %== [7, 3] // true

(If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)

milos
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Thank you for giving this a thought!

milos

···

On Apr 6, 2016, at 10:13 AM, Milos Rankovic via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Checking for divisibility is very common:

21 % 3 == 0 // true

In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:

21 %== 3 // true

… which also allows me to overload it for sequences:

21 %== [7, 3] // true

(If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)

While modulo checks are common, I don't think that your proposed solution (%==) enhances readability or saves typing *to such extent* that it vastly improves over the existing art:

21 % 3 == 0 reads easily from left to right, is quick to type, is understood across many languages.

21 %== 3 saves a few spaces, is less immediately understandable (due to the visual overlap with `+=` and `-=`) and would be (as far as I'm aware of) unique to Swift.

I applaud the thinking and creativity but I would not support the proposal.

-- E

I'm not suggesting this for the standard library (or anyone's production
code), but it's fun to note that there is actually a standard mathematical
notation for this, and it has a corresponding Unicode character U+2223
"DIVIDES": ∣

infix operator ∣ {}
func ∣<I: IntegerType>(lhs: I, rhs: I) -> Bool {
return rhs % lhs == 0
}

3 ∣ 12 // true
4 ∣ 12 // true
5 ∣ 12 // false

Jacob

···

On Thu, Apr 7, 2016 at 11:13 AM, Milos Rankovic via swift-evolution < swift-evolution@swift.org> wrote:

Thank you for giving this a thought!

milos

On Apr 6, 2016, at 10:13 AM, Milos Rankovic via swift-evolution < > swift-evolution@swift.org> wrote:

Checking for divisibility is very common:

21 % 3 == 0 // true

In fact, this is such a common use of the `%` operator that the `== 0`
side of the expression seems distracting in this use case. For quite a
while now, I’ve been using a custom operator for this, which is steadily
growing on me:

21 %== 3 // true

… which also allows me to overload it for sequences:

21 %== [7, 3] // true

(If I’m inadvertently misusing this mailing list to share such a minor
idea, please tell me off so that I can learn not to do it again!)

While modulo checks are common, I don't think that your proposed solution
(%==) enhances readability or saves typing **to such extent** that it
vastly improves over the existing art:

21 % 3 == 0 reads easily from left to right, is quick to type, is
understood across many languages.

21 %== 3 saves a few spaces, is less immediately understandable (due
to the visual overlap with `+=` and `-=`) and would be (as far as I'm aware
of) unique to Swift.

I applaud the thinking and creativity but I would not support the proposal.

-- E

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

And a Unicode character too! That’s great to know, Jacob, thanks!

milos

···

On 11 Apr 2016, at 02:39, Jacob Bandes-Storch <jtbandes@gmail.com> wrote:

I'm not suggesting this for the standard library (or anyone's production code), but it's fun to note that there is actually a standard mathematical notation for this, and it has a corresponding Unicode character U+2223 "DIVIDES": ∣