diff options
author | michelou <michelou@epfl.ch> | 2007-10-05 16:48:02 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2007-10-05 16:48:02 +0000 |
commit | 41865ed00183a049d6fbaebc97897eebd2956d68 (patch) | |
tree | 4d88344154774c1a95046f8f22771d6f7a5f0e72 | |
parent | 6761dc14b707667b453455aa6cecb0c82babeed3 (diff) | |
download | scala-41865ed00183a049d6fbaebc97897eebd2956d68.tar.gz scala-41865ed00183a049d6fbaebc97897eebd2956d68.tar.bz2 scala-41865ed00183a049d6fbaebc97897eebd2956d68.zip |
fixed ticket #153
-rw-r--r-- | src/library/scala/Stream.scala | 14 | ||||
-rw-r--r-- | test/files/run/streams.check | 1 | ||||
-rw-r--r-- | test/files/run/streams.scala | 5 |
3 files changed, 16 insertions, 4 deletions
diff --git a/src/library/scala/Stream.scala b/src/library/scala/Stream.scala index 5a4a705a61..034c9aa70f 100644 --- a/src/library/scala/Stream.scala +++ b/src/library/scala/Stream.scala @@ -414,13 +414,19 @@ trait Stream[+A] extends Seq.Projection[A] { * @return <code>f(a<sub>0</sub>) ::: ... ::: f(a<sub>n</sub>)</code> if * this stream is <code>[a<sub>0</sub>, ..., a<sub>n</sub>]</code>. */ - override def flatMap[B](f: A => Iterable[B]): Stream[B] = - if (isEmpty) Stream.empty - else (f(head)).toStream append tail.flatMap(f) + override def flatMap[B](f: A => Iterable[B]): Stream[B] = { + var acc: Stream[B] = Stream.empty + var these = this + while (!these.isEmpty) { + val those = f(these.head) + if (!those.isEmpty) acc = acc append those.toStream + these = these.tail + } + acc + } override def toStream = this - /** A stream consisting of all elements of this stream in reverse order. */ override def reverse: Stream[A] = diff --git a/test/files/run/streams.check b/test/files/run/streams.check index 04d92425d9..d11eb7f67a 100644 --- a/test/files/run/streams.check +++ b/test/files/run/streams.check @@ -15,3 +15,4 @@ Stream() Stream(1, 2) Stream() +65536 diff --git a/test/files/run/streams.scala b/test/files/run/streams.scala index cd1185344b..408affe306 100644 --- a/test/files/run/streams.scala +++ b/test/files/run/streams.scala @@ -21,4 +21,9 @@ object Test extends Application { println(s2.drop(-1)) println(s2.dropWhile(_ > 0)) println + + // ticket #153 + def powers(x: Int) = if ((x&(x-1)) == 0) Some(x) else None + val s3 = Stream.range(1, 100000) + println(s3.flatMap(powers).reverse.head) } |