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/TraversableOnce.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/TraversableOnce.scala')
-rw-r--r-- | src/library/scala/collection/TraversableOnce.scala | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/library/scala/collection/TraversableOnce.scala b/src/library/scala/collection/TraversableOnce.scala index 179051553e..4836f2666c 100644 --- a/src/library/scala/collection/TraversableOnce.scala +++ b/src/library/scala/collection/TraversableOnce.scala @@ -9,6 +9,7 @@ package scala.collection import mutable.{ Buffer, ListBuffer, ArrayBuffer } +import annotation.unchecked.{ uncheckedVariance => uV } /** A template trait for collections which can be traversed either once only * or one or more times. @@ -88,7 +89,6 @@ trait TraversableOnce[+A] { def exists(p: A => Boolean): Boolean def find(p: A => Boolean): Option[A] def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Unit - // def mapFind[B](f: A => Option[B]): Option[B] // for internal use protected[this] def reversed = { @@ -128,6 +128,25 @@ trait TraversableOnce[+A] { cnt } + /** Finds the first element of the $coll for which the given partial + * function is defined, and applies the partial function to it. + * + * $mayNotTerminateInf + * $orderDependent + * + * @param pf the partial function + * @return an option value containing pf applied to the first + * value for which it is defined, or `None` if none exists. + * @example `Seq("a", 1, 5L).collectFirst({ case x: Int => x*10 }) = Some(10)` + */ + def collectFirst[B](pf: PartialFunction[A, B]): Option[B] = { + for (x <- self.toIterator) { + if (pf isDefinedAt x) + return Some(pf(x)) + } + None + } + /** Applies a binary operator to a start value and all elements of this $coll, * going left to right. * @@ -350,6 +369,19 @@ trait TraversableOnce[+A] { reduceLeft((x, y) => if (cmp.gteq(x, y)) x else y) } + def maxBy[B](f: A => B)(implicit cmp: Ordering[B]): A = { + if (isEmpty) + throw new UnsupportedOperationException("empty.maxBy") + + reduceLeft((x, y) => if (cmp.gteq(f(x), f(y))) x else y) + } + def minBy[B](f: A => B)(implicit cmp: Ordering[B]): A = { + if (isEmpty) + throw new UnsupportedOperationException("empty.maxBy") + + reduceLeft((x, y) => if (cmp.lteq(f(x), f(y))) x else y) + } + /** Copies all elements of this $coll to a buffer. * $willNotTerminateInf * @param dest The buffer to which elements are copied. @@ -628,8 +660,8 @@ object TraversableOnce { class TraversableOnceMonadOps[+A](trav: TraversableOnce[A]) { def map[B](f: A => B): TraversableOnce[B] = trav.toIterator map f def flatMap[B](f: A => TraversableOnce[B]): TraversableOnce[B] = trav.toIterator flatMap f - def filter(p: A => Boolean): TraversableOnce[A] = trav.toIterator filter p - def withFilter(p: A => Boolean) = filter(p) + def withFilter(p: A => Boolean) = trav.toIterator filter p + def filter(p: A => Boolean): TraversableOnce[A] = withFilter(p) } } |