New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Range type #2490
base: v2.0.0
Are you sure you want to change the base?
Added Range type #2490
Conversation
Codecov Report
@@ Coverage Diff @@
## v2.0.0 #2490 +/- ##
============================================
- Coverage 90.69% 81.67% -9.03%
- Complexity 166 167 +1
============================================
Files 11 12 +1
Lines 344 382 +38
Branches 65 75 +10
============================================
Hits 312 312
- Misses 32 70 +38
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your solution is much less verbose than mine 👍
static Range<Integer> inclusiveBy(int from, int toInclusive, int step) { | ||
if (step == 0) { | ||
throw new IllegalArgumentException("step cannot be 0"); | ||
} else if (from == toInclusive) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you consider removing some else
?
if (step == 0) {
throw new IllegalArgumentException("step cannot be 0");
}
if (from == toInclusive) {
return () -> Iterator.of(from);
}
if (step > 0) {
...
} else {
...
}
var signum = Integer.signum(step); | ||
var toInclusive = toExclusive - signum; | ||
if (Integer.signum(toInclusive) != Integer.signum(toExclusive)) { | ||
// because of abs(signum) <= abs(step) and overflow detection, toExclusive will not be included |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have all the existing tests running? I think I understand your approach, but it's very complex... Personally, I prefer not rely on inclusiveBy
...
|
||
} | ||
|
||
abstract class AbstractRangeIterator implements Iterator<Integer> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is elegant, combined with the abstract hasNext()
👍
} | ||
|
||
static Range<Integer> exclusive(int from, int toExclusive) { | ||
return Range.exclusiveBy(from, toExclusive, from <= toExclusive ? 1 : -1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think about using another implementation for step=1? The overflow detection can be avoided, which can improve performance, as I believe most people are using range without giving step (step=1). JDK does it without overflow detection with step=1, see java.util.stream.Streams.RangeIntSpliterator
.
I really like a a. Write Javadoc. b. Store
c. Something controversial:
|
@ullenboom Thanks for your suggestions. The PR is marked as Draft / WIP.
This is part of our definition of done. You can expect something, otherwise the PR will not be pulled :)
If you mean making Range a class instead of an interface: yes, I plan to do that. Probably I will align to Scala. See https://www.scala-lang.org/api/current/scala/collection/immutable/Range.html
Yes, in Scala an exclusive end is also the default. There the syntax is The inclusive end is denoted (Additionally, there are method overloads for the step parameters).
In Scala, |
No description provided.