diff options
-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. |