diff options
author | Jean-Remi Desjardins <jeanremi.desjardins@gmail.com> | 2012-11-22 00:59:43 -0500 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-12-28 16:56:06 -0800 |
commit | 24a033b2aa4174fe8e9c3c02372b6508ef404601 (patch) | |
tree | 11576d7d56057ad565af82a0896fb4a41d4d3e36 /src/library | |
parent | 1284c3c6f38cc419a0a39fd68b3d5cf81b36b1a5 (diff) | |
download | scala-24a033b2aa4174fe8e9c3c02372b6508ef404601.tar.gz scala-24a033b2aa4174fe8e9c3c02372b6508ef404601.tar.bz2 scala-24a033b2aa4174fe8e9c3c02372b6508ef404601.zip |
SI-6415, overly eager evaluation in Stream.
The lengthCompare method in LinearSeqOptimized was looking one
step further than it needed to in order to give the correct
result, which was creating some unwanted side effects related to
Streams.
Diffstat (limited to 'src/library')
-rwxr-xr-x | src/library/scala/collection/LinearSeqOptimized.scala | 20 | ||||
-rw-r--r-- | src/library/scala/collection/SeqLike.scala | 16 |
2 files changed, 23 insertions, 13 deletions
diff --git a/src/library/scala/collection/LinearSeqOptimized.scala b/src/library/scala/collection/LinearSeqOptimized.scala index 0f0a405a85..48b0b5469e 100755 --- a/src/library/scala/collection/LinearSeqOptimized.scala +++ b/src/library/scala/collection/LinearSeqOptimized.scala @@ -247,14 +247,20 @@ trait LinearSeqOptimized[+A, +Repr <: LinearSeqOptimized[A, Repr]] extends Linea } override /*SeqLike*/ - def lengthCompare(len: Int): Int = { - var i = 0 - var these = self - while (!these.isEmpty && i <= len) { - i += 1 - these = these.tail + def lengthCompare(len: Int): Int = { + // TODO: Remove this method when incrementing a major revision + // This method is the same as in SeqLike, no need to redefine it + if (len < 0) 1 + else { + var i = 0 + val it = iterator + while (it.hasNext) { + if (i == len) return if (it.hasNext) 1 else 0 + it.next() + i += 1 + } + i - len } - i - len } override /*SeqLike*/ diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala index f65e2ef9cd..1be0dba29f 100644 --- a/src/library/scala/collection/SeqLike.scala +++ b/src/library/scala/collection/SeqLike.scala @@ -84,13 +84,17 @@ trait SeqLike[+A, +Repr] extends Any with IterableLike[A, Repr] with GenSeqLike[ * if computing `length` is cheap. */ def lengthCompare(len: Int): Int = { - var i = 0 - val it = iterator - while (it.hasNext && i <= len) { - it.next() - i += 1 + if (len < 0) 1 + else { + var i = 0 + val it = iterator + while (it.hasNext) { + if (i == len) return if (it.hasNext) 1 else 0 + it.next() + i += 1 + } + i - len } - i - len } override /*IterableLike*/ def isEmpty: Boolean = lengthCompare(0) == 0 |