diff options
author | Paul Phillips <paulp@improving.org> | 2009-08-30 03:36:52 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-08-30 03:36:52 +0000 |
commit | 61635f0f584ac1b8a10ef2fff238b39aa7d21b83 (patch) | |
tree | 1b8121477e84c6b4fb0c7c50dab0314e5235610c | |
parent | 81440d55eea6309dd7e2101f5467dfce20a46a9a (diff) | |
download | scala-61635f0f584ac1b8a10ef2fff238b39aa7d21b83.tar.gz scala-61635f0f584ac1b8a10ef2fff238b39aa7d21b83.tar.bz2 scala-61635f0f584ac1b8a10ef2fff238b39aa7d21b83.zip |
Blessing Iterator with another method from the ...
Blessing Iterator with another method from the Traversables.
-rw-r--r-- | src/library/scala/collection/Iterator.scala | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 0ae5eda848..4212d14eb3 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -12,7 +12,7 @@ package scala.collection import mutable.{Buffer, ArrayBuffer, ListBuffer} -import annotation.tailrec +import annotation.{ tailrec, experimental } // import immutable.{List, Nil, ::, Stream} /** The <code>Iterator</code> object provides various functions for @@ -361,6 +361,22 @@ trait Iterator[+A] { self => } } + /** Returns a new iterator based on the partial function <code>pf</code>, + * containing pf(x) for all the elements which are defined on pf. + * The order of the elements is preserved. + * @param pf the partial function which filters and maps the iterator. + * @return the new iterator. + */ + @experimental + def filterMap[B](pf: PartialFunction[Any, B]): Iterator[B] = { + val self = buffered + new Iterator[B] { + private def skip() = while (self.hasNext && !pf.isDefinedAt(self.head)) self.next() + def hasNext = { skip(); self.hasNext } + def next() = { skip(); pf(self.next()) } + } + } + /** Returns an iterator over the longest prefix of this iterator such that * all elements of the result satisfy the predicate <code>p</code>. * The order of the elements is preserved. |