Hacker News new | ask | show | jobs
by nextaccountic 1772 days ago
in rust you would write (0 .. 99).sum() for range not including 99

or (0 ..= 99).sum() for range including the 99

which makes me think that i don't know how to read (0 to 99) unless i learn whether it's exclusive or inclusive (but at least it's googlable, unlike a random looking operator)

1 comments

The "to" method on an Int creates an inclusive Range.

There is also an "until" method which would create the noninclusive Range (which would be actually closer to the original code as it would be "0 until 100").

The Rust syntax is not better. Maybe you could come up what means what if you see it in comparison (this would also work maybe for the Scala code).

The K example has even two implicit assumptions: It's an noninclusive range starting at zero. (They write on the Wikipedia page where I looked this up, as I don't know K, that the range is over "nonnegative integers" lower than the given one. But "nonnegative integers" means usually, but not always, the natural numbers, so zero not included; this makes it even more inconclusive than the Rust or Scala code snippet, imho. I had to look on examples to find out whether zero is included or not in "!").

Nonnegative always includes zero, unless the author had muddled thinking themselves. Since positive is >0 and negative is <0, their negations are nonpositive for <=0 and nonnegative for >=0.
OH! That's of course right.

I wasn't precise enough. The Wikipedia page talks actually about "positive" integers, and I transformed this in my head to what should be written there.

Original quote:

> !x enumerate the positive integers less than x.