diff options
author | Som Snytt <som.snytt@gmail.com> | 2014-05-27 12:06:56 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2014-05-27 12:06:56 -0700 |
commit | d4f664c98364858c319b599caa1cdbb68030440a (patch) | |
tree | 65f9efb179460384b5ddb224faf574be6d8ca52c /src | |
parent | 5551cf66e5b27ae398b527df6fe4247aed1ff307 (diff) | |
download | scala-d4f664c98364858c319b599caa1cdbb68030440a.tar.gz scala-d4f664c98364858c319b599caa1cdbb68030440a.tar.bz2 scala-d4f664c98364858c319b599caa1cdbb68030440a.zip |
SI-8475 GroupedIterator is also lazy when padded
This is the related case which dnlgtm in the previous fix.
The old code seems to be tilting for laziness, but we have
to fill the buffer with the current group anyway, so there
is no reason to be coy about calling ArrayBuffer.length.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/Iterator.scala | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index e321a6adba..f6f46e158f 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -922,6 +922,9 @@ trait Iterator[+A] extends TraversableOnce[A] { /** For reasons which remain to be determined, calling * self.take(n).toSeq cause an infinite loop, so we have * a slight variation on take for local usage. + * NB: self.take.toSeq is slice.toStream, lazily built on self, + * so a subsequent self.hasNext would not test self after the + * group was consumed. */ private def takeDestructively(size: Int): Seq[A] = { val buf = new ArrayBuffer[A] @@ -945,12 +948,10 @@ trait Iterator[+A] extends TraversableOnce[A] { // so the rest of the code can be oblivious val xs: Seq[B] = { val res = takeDestructively(count) - // extra checks so we don't calculate length unless there's reason - if (pad.isDefined && !self.hasNext) { - val shortBy = count - res.length - if (shortBy > 0) res ++ padding(shortBy) else res - } - else res + // was: extra checks so we don't calculate length unless there's reason + // but since we took the group eagerly, just use the fast length + val shortBy = count - res.length + if (shortBy > 0 && pad.isDefined) res ++ padding(shortBy) else res } lazy val len = xs.length lazy val incomplete = len < count |