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
Fix slicing of views of IndexedSeq
s (including fixing 2.13.7-only reverseIterator
regression)
#9799
Conversation
review by @scala/collections ? |
reverseIterator
on IndexedSeq
s)
If collections drops by, so to speak, maybe they could indicate whether it was intentional that |
I see Stefan reverted the change in laziness for "An iterator for slicing that does not drop eagerly." at #4086 The paulp tweets there are dead links. |
title should be "fixed slice of indexed seq view [...]" |
reverseIterator
on IndexedSeq
s)IndexedSeq
s (including fixing 2.13.7-only reverseIterator
regression)
A plain fix is of course mergeable, but it would be awesome if someone could plug the apparent hole in https://github.com/scala/scala-collection-laws too |
Unfortunately, I broke another test-at-a-distance. I'll also look into The Laws. I didn't feel great tweaking the innards without test coverage. |
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.
while certainly simpler and a lot easier to follow, I'm not certain that the new implementation of IndexedSeqViewReverseIterator#sliceIterator
is correct. also, I think we probably need more tests for sliceIterator
, since I didn't see anything directly related to it fail
Fix incorrect internal usage of drop. Optimize slice of empty and single Iterator. Simplify reverse view iterator slice.
some laws that might be useful to scalacheck: forAll { (v: Vector[Int], n0: Int) =>
val n = n0 & Int.MaxValue // make it non-neg
assert(v.iterator.drop(n).toList == v.toList.drop(n))
}
forAll { (v: Vector[Int], n: Int) =>
val n = n0 & Int.MaxValue // make it non-neg
assert(v.iterator.take(n).toList == v.toList.take(n))
}
forAll { (v: Vector[Int], n: Int) =>
val n = n0 & Int.MaxValue // make it non-neg
assert(v.reverseIterator.drop(n).toList == v.toList.reverse.drop(n))
}
forAll { (v: Vector[Int], n: Int) =>
val n = n0 & Int.MaxValue // make it non-neg
assert(v.reverseIterator.take(n).toList == v.toList.reverse.take(n))
}
forAll { (v: Vector[Int], a0: Int, b0: Int) =>
val a1 = a0 & Int.MaxValue // make it non-neg
val b1 = b0 & Int.MaxValue // make it non-neg
val (a, b) = if (a <= b) (a1, b1) else (b1, a1)
assert(v.view.slice(a, b).toList == v.slice(a, b).toList)
} |
978a527
to
4ece433
Compare
@NthPortal there are optimized small Map iterators from last year (to avoid calling Iterator.apply). They override drop to just bump a count; that is probably where it came up to return this.type. But that only makes sense for iterating over these pre-computed values. These iterators do not override The failing test was to check that empty and single Maps don't allocate on iterating after drop; the fix was to make |
@johnynek - The property tests in the Scala Collections Laws repository do not use scalacheck because they instead do code generation to also check type inference and such. It would also be possible to write more property tests in scalacheck. @som-snytt - Collections Laws are rather idiosyncratic. Let me know if I should try to carve out time to tackle this part. I don't remember the code much, but I guess "not much" is better than none, and I have the advantage that I still probably think like 2014-me. (Or whenever that was.) |
@SethTisue I'm sorry for making noise here (I don't know an appropriate channel) but can you remove me from the collections group? I don't have time to do these reviews at the moment. |
@johnynek done! thanks for all the help you've provided these past months, it's much appreciated 🙇 |
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.
I still think a few more tests for sliceIterator
(directly or indirectly) would be a good idea; otherwise, LGTM
Thank you all 👍 |
Another follow-up could be to remove the scala/src/library/scala/collection/IndexedSeqView.scala Lines 66 to 72 in 2131f5b
|
Roger that. Collections are more eager (so to speak) to override drop than take.
In this context, I would have expected more symmetry, but I'll report again in the follow-up PR. |
Fixes scala/bug#12479