Range.closed(1,5) == [1,5]
Range.open(1,5) == (1,5)
Range.openClosed(1,5) == (1,5]
Range.closedOpen(1,5) == [1,5)
Range.greaterThan(1) == (1,infinity)
Range.atLeast(1) == [1,infinity)
That class always strikes me as having high power-to-weight. It has methods like encloses(anotherRange), contains(aValue), and others.
https://google.github.io/guava/releases/19.0/api/docs/com/go...
Range.closed(1,5) is the syntax used to refer to what a mathematician would call [1,5].
Guava is just a Java library. In Java 9, a two-Integer-element list literal will be List.of(9000, 9001).
EDIT: Genuinely curious, this is not meant to troll or start a war.
I like the more explicit feeling `count-1`. I'd use either...
as for the "count - 1", it's not too bad because i find "inclusive" more intuitive than "exclusive" for the a..b notation, and that does make it explicit that you're not including b, but it's visual clutter in much the same way that having to iterate an array via "for i = 0 to len - 1" is.
When I see "0..<n" I know the range is exclusive of n.
Also, exclusivity is almost always what you want, so "0..n" should just default to exclusivity and "0...n" could be inclusive.
http://stackoverflow.com/questions/9690801/difference-betwee...
I always remembered it as "the third dot makes it bigger so it pushes the range and the last item falls off". I can't remember where that came from, perhaps related to the poignant guide:
http://poignant.guide/book/chapter-3.html#section2
I actually find in practice the swift ranges are the only ones I can reliably use without having to stop and look up the reference syntax every time.
For some reason, for me, `0..<n` reads as "0 through less than n" which signals to my brain a clear signal of an exclusive range.
Then I can work from there and if it doesn't have the < symbol it must be exclusive.
I suspect `0..=n` would work similarly well, but I've not seen a real language to ever do that yet
"0..<count" instantly tells me that it's not including value, and follows the matematical interval notification [0..count] vs [0..count)
0...1 //0 to 1
Note: triple dot. 0..2 //error: repl.swift:1:2: error: use of unresolved operator '..'
0..<1 //0 to <1 0 ..< count
I think Swift programmers should adopt this style.How's that different from:
for (int i=0; i<=count; i++)
in e.g. C99? count.forEach {
// do things
}OTOH, a "loop .. for .. {upto, below} count" leaves less questions in a casual code review :)
scala> 1 to 3
scala.collection.immutable.Range.Inclusive = Range(1, 2, 3)
scala> 1 until 3
scala.collection.immutable.Range = Range(1, 2)
> 1 through 3
# [1, 2, 3]
> 1 until 3 # or 1 to 3
# [1, 2]
as explicit word range notation?