diff options
author | Paul Phillips <paulp@improving.org> | 2011-05-14 23:00:05 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-05-14 23:00:05 +0000 |
commit | d5c7021dd7197b373a80b856cc733f57de8a8062 (patch) | |
tree | c24bc537b1c5ab4345a50f9486717d795318cabb | |
parent | feb1ba8ab3fed03d498ab768e07e0a27805a5f58 (diff) | |
download | scala-d5c7021dd7197b373a80b856cc733f57de8a8062.tar.gz scala-d5c7021dd7197b373a80b856cc733f57de8a8062.tar.bz2 scala-d5c7021dd7197b373a80b856cc733f57de8a8062.zip |
Fix for Iterator flattening regression.
but my theory is that ++ takes a by name argument, but doing a foldLeft
and using ++ to join creates a closure which loses the by-nameness. If
this theory is correct that's an ugly trap.
Not sure how I write a test against this sort of thing? Will take
pointers. For now, closes #4582, no review.
-rw-r--r-- | src/library/scala/collection/TraversableOnce.scala | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/library/scala/collection/TraversableOnce.scala b/src/library/scala/collection/TraversableOnce.scala index 90f1583c58..84fe4bdf4c 100644 --- a/src/library/scala/collection/TraversableOnce.scala +++ b/src/library/scala/collection/TraversableOnce.scala @@ -346,7 +346,12 @@ object TraversableOnce { } class FlattenOps[A](travs: TraversableOnce[TraversableOnce[A]]) { - def flatten: Iterator[A] = travs.foldLeft(Iterator.empty: Iterator[A])(_ ++ _) + def flatten: Iterator[A] = new Iterator[A] { + val its = travs.toIterator + private var it: Iterator[A] = Iterator.empty + def hasNext: Boolean = it.hasNext || its.hasNext && { it = its.next.toIterator; hasNext } + def next(): A = if (hasNext) it.next() else Iterator.empty.next() + } } class MonadOps[+A](trav: TraversableOnce[A]) { |