-
Notifications
You must be signed in to change notification settings - Fork 98
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
keep fixing warnings in 'core' #571
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,14 @@ | |
|
||
package cats.collections | ||
|
||
import cats.{Eq, Order, Show} | ||
import cats.Eq | ||
import cats.Order | ||
import cats.Show | ||
|
||
/** | ||
* Represent an inclusive range [x, y] that can be generated by using discrete operations | ||
*/ | ||
final case class Range[A](val start: A, val end: A) { | ||
final case class Range[A](start: A, end: A) { | ||
|
||
/** | ||
* Subtract a Range from this range. The result will be 0, 1 or 2 ranges | ||
|
@@ -77,13 +79,12 @@ final case class Range[A](val start: A, val end: A) { | |
* the start's successor. | ||
*/ | ||
def toIterator(implicit discrete: Discrete[A], order: Order[A]): Iterator[A] = { | ||
val next: A => A = a => if (order.lteqv(a, end)) discrete.succ(a) else discrete.pred(a) | ||
val next: A => A = if (order.lt(start, end)) discrete.succ(_) else discrete.pred(_) | ||
|
||
Stream | ||
Iterator | ||
.iterate(start)(next) | ||
.takeWhile(concurrent => !order.eqv(concurrent, end)) | ||
.append(Seq(end)) | ||
.iterator | ||
.takeWhile(order.neqv(_, end)) | ||
.++(Iterator.single(end)) | ||
} | ||
|
||
/** | ||
|
@@ -103,14 +104,18 @@ final case class Range[A](val start: A, val end: A) { | |
def contains(x: A)(implicit A: Order[A]): Boolean = A.gteqv(x, start) && A.lteqv(x, end) | ||
|
||
/** | ||
* Apply function f to each element in range [star, end] | ||
* Apply function f to each element in range `[star, end]`. Does nothing if `end` precedes `start`. | ||
*/ | ||
def foreach(f: A => Unit)(implicit discrete: Discrete[A], order: Order[A]): Unit = { | ||
var i = start | ||
while (order.lteqv(i, end)) { | ||
// Visit values in range [start, end), i.e. excluding `end`. | ||
while (order.lt(i, end)) { | ||
f(i) | ||
i = discrete.succ(i) | ||
} | ||
// Visit the last (or the only) value, if any. | ||
if (order.eqv(i, end)) | ||
f(i) | ||
Comment on lines
+111
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous implementation was resulting to infinite loops in cases like
I.e. due to integer overflows that could not be caught previously. |
||
} | ||
|
||
def map[B](f: A => B): Range[B] = Range[B](f(start), f(end)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Pretty much the same fix as it was in
Diet.scala
for the "match may not be exhaustive" warnings.