diff options
author | Paul Phillips <paulp@improving.org> | 2010-12-04 07:55:49 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-12-04 07:55:49 +0000 |
commit | 1a45bc7f19a56bd7959a496f617b501d1eae7513 (patch) | |
tree | 0720d9e9b17414ced6c02bbdea147fb18062e5b6 /src/library/scala/collection/Iterator.scala | |
parent | 9e9914e109c91cd4f86802129c236827517d8386 (diff) | |
download | scala-1a45bc7f19a56bd7959a496f617b501d1eae7513.tar.gz scala-1a45bc7f19a56bd7959a496f617b501d1eae7513.tar.bz2 scala-1a45bc7f19a56bd7959a496f617b501d1eae7513.zip |
A selection of collections additions from the l...
A selection of collections additions from the lower end of the
controversy scale.
// TraversableOnce
def collectFirst[B](pf: PartialFunction[A, B]): Option[B]
def maxBy[B](f: A => B)(implicit cmp: Ordering[B]): A
def minBy[B](f: A => B)(implicit cmp: Ordering[B]): A
// Iterator
def span(p: A => Boolean): (Iterator[A], Iterator[A])
// Traversable
def inits: Iterator[Repr]
def tails: Iterator[Repr]
def unzip3[A1, A2, A3](implicit asTriple: A => (A1, A2, A3)): (CC[A1], CC[A2], CC[A3])
// Sequences
def permutations: Iterator[Repr]
Review by odersky.
Diffstat (limited to 'src/library/scala/collection/Iterator.scala')
-rw-r--r-- | src/library/scala/collection/Iterator.scala | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 31ab2a0eb4..40f8bc7f81 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -461,7 +461,7 @@ trait Iterator[+A] extends TraversableOnce[A] { val self = buffered class PartitionIterator(p: A => Boolean) extends Iterator[A] { var other: PartitionIterator = _ - val lookahead = new scala.collection.mutable.Queue[A] + val lookahead = new mutable.Queue[A] def skip() = while (self.hasNext && !p(self.head)) { other.lookahead += self.next @@ -477,6 +477,48 @@ trait Iterator[+A] extends TraversableOnce[A] { (l, r) } + /** Splits this Iterator into a prefix/suffix pair according to a predicate. + * + * @param p the test predicate + * @return a pair of Iterators consisting of the longest prefix of this + * whose elements all satisfy `p`, and the rest of the Iterator. + */ + def span(p: A => Boolean): (Iterator[A], Iterator[A]) = { + val self = buffered + val leading = new Iterator[A] { + private var isDone = false + val lookahead = new mutable.Queue[A] + def advance() = { + self.hasNext && p(self.head) && { + lookahead += self.next + true + } + } + def finish() = { + while (advance()) () + isDone = true + } + def hasNext = lookahead.nonEmpty || advance() + def next() = { + if (lookahead.isEmpty) + advance() + + lookahead.dequeue() + } + } + val trailing = new Iterator[A] { + private lazy val it = { + leading.finish() + self + } + def hasNext = it.hasNext + def next() = it.next() + override def toString = "unknown-if-empty iterator" + } + + (leading, trailing) + } + /** Skips longest sequence of elements of this iterator which satisfy given * predicate `p`, and returns an iterator of the remaining elements. * @@ -642,21 +684,6 @@ trait Iterator[+A] extends TraversableOnce[A] { res } - /** Applies option-valued function to successive elements of this iterator - * until a defined value is found. - * - * @param f the function to be applied to successive elements. - * @return an option value containing the first defined result of - * `f`, or `None` if `f` returns `None` for all all elements. - def mapFind[B](f: A => Option[B]): Option[B] = { - var res: Option[B] = None - while (res.isEmpty && hasNext) { - res = f(next()) - } - res - } - */ - /** Returns the index of the first produced value satisfying a predicate, or -1. * $mayNotTerminateInf * @param p the predicate to test values |