diff options
author | Martin Odersky <odersky@gmail.com> | 2008-07-21 15:31:03 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2008-07-21 15:31:03 +0000 |
commit | b894f804ad22bc1c16610785f34b62efcea4a514 (patch) | |
tree | 1687efce0bae26cfe83a572a9ddd491a8aab3cde /src/library | |
parent | aeb29ddfbb4ed2000c6e8ed2bc80619e2c157aff (diff) | |
download | scala-b894f804ad22bc1c16610785f34b62efcea4a514.tar.gz scala-b894f804ad22bc1c16610785f34b62efcea4a514.tar.bz2 scala-b894f804ad22bc1c16610785f34b62efcea4a514.zip |
added Iterator.flatten method; refactoring: clo...
added Iterator.flatten method; refactoring: closure -> baseTypeSeq
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Iterator.scala | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index 8d5c04f329..85d9a8a238 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -191,6 +191,22 @@ object Iterator { override def hasNext: Boolean = true def next(): Int = { val j = i; i = step(i); j } } + + /** Create an iterator that is the concantenation of all iterators + * returned by a given iterator of iterators. + * @param its The iterator which returns on each call to next + * a new iterator whose elements are to be concatenated to the result. + */ + def flatten[T](its: Iterator[Iterator[T]]): Iterator[T] = new Iterator[T] { + private var it = its.next + def hasNext: Boolean = { + while (!it.hasNext && its.hasNext) it = its.next + it.hasNext + } + def next(): T = + if (hasNext) it.next + else empty.next() + } } /** Iterators are data structures that allow to iterate over a sequence |