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/scala/collection/SeqLike.scala | |
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/scala/collection/SeqLike.scala')
-rw-r--r-- | src/library/scala/collection/SeqLike.scala | 16 |
1 files changed, 10 insertions, 6 deletions
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 |