Added (a variation on) jorge ortiz's sortBy to SeqLike,
added docs and test case. Added map to Ordering. git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@19244 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
parent
919d2986aa
commit
a785301e72
|
@ -91,6 +91,11 @@ trait Ordering[T] extends Comparator[T] with PartialOrdering[T] {
|
|||
override def reverse = outer
|
||||
def compare(x: T, y: T) = outer.compare(y, x)
|
||||
}
|
||||
|
||||
/** Given a function mapping U => T, creates Ordering[U]. */
|
||||
def map[U](f: U => T): Ordering[U] = new Ordering[U] {
|
||||
def compare(x: U, y: U) = outer.compare(f(x), f(y))
|
||||
}
|
||||
|
||||
class Ops(lhs: T) {
|
||||
def <(rhs: T) = lt(lhs, rhs)
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
package scala.collection
|
||||
import generic._
|
||||
import mutable.{ListBuffer, HashMap, GenericArray}
|
||||
import annotation.experimental
|
||||
|
||||
// import immutable.{List, Nil, ::}
|
||||
import generic._
|
||||
|
@ -207,7 +208,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
|
|||
def findIndexOf(p: A => Boolean): Int = indexWhere(p)
|
||||
|
||||
/** Returns the index of the first occurence of the specified
|
||||
* object in this iterable object.
|
||||
* object in this sequence.
|
||||
*
|
||||
* @note may not terminate for infinite-sized collections.
|
||||
* @param elem element to search for.
|
||||
|
@ -218,7 +219,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
|
|||
def indexOf[B >: A](elem: B): Int = indexOf(elem, 0)
|
||||
|
||||
/** Returns the index of the first occurence of the specified
|
||||
* object in this iterable object, starting from a start index, or
|
||||
* object in this sequence, starting from a start index, or
|
||||
* -1, if none exists.
|
||||
*
|
||||
* @note may not terminate for infinite-sized collections.
|
||||
|
@ -530,7 +531,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
|
|||
b.result
|
||||
}
|
||||
|
||||
/** Sort the iterable according to the comparison function
|
||||
/** Sort the sequence according to the comparison function
|
||||
* <code><(e1: a, e2: a) => Boolean</code>,
|
||||
* which should be true iff <code>e1</code> is smaller than
|
||||
* <code>e2</code>.
|
||||
|
@ -538,7 +539,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
|
|||
* same order in the sorted sequence as in the original.
|
||||
*
|
||||
* @param lt the comparison function
|
||||
* @return an iterable sorted according to the comparison function
|
||||
* @return a sequence sorted according to the comparison function
|
||||
* <code><(e1: a, e2: a) => Boolean</code>.
|
||||
* @ex <pre>
|
||||
* List("Steve", "Tom", "John", "Bob")
|
||||
|
@ -560,6 +561,24 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
|
|||
for (x <- arr) b += x
|
||||
b.result
|
||||
}
|
||||
|
||||
/** Sort the sequence according to the Ordering which results from mapping
|
||||
* the implicitly given Ordering[B] to an Ordering[A]. For example:
|
||||
*
|
||||
* <code>
|
||||
* val words = "The quick brown fox jumped over the lazy dog".split(' ')
|
||||
* // this works because scala.Ordering will implicitly provide an Ordering[Tuple2[Int, Char]]
|
||||
* words.sortBy(x => (x.length, x.head.toLower))
|
||||
* res0: Array[String] = Array(dog, fox, The, the, lazy, over, brown, quick, jumped)
|
||||
* </code>
|
||||
*
|
||||
* @param f the mapping function
|
||||
* @param ord the Ordering[B]
|
||||
* @return the sorted representation
|
||||
*/
|
||||
|
||||
@experimental
|
||||
def sortBy[B](f: A => B)(implicit ord: Ordering[B]): Repr = sortWith(ord map f)
|
||||
|
||||
/**
|
||||
* Overridden for efficiency.
|
||||
|
|
|
@ -27,5 +27,10 @@ object Test extends Application {
|
|||
testAll[Iterable[Int]](List(1), List(1, 2));
|
||||
testAll[Iterable[Int]](List(1, 2), List(2));
|
||||
testAll((1, "bar"), (1, "foo"))
|
||||
testAll((1, "foo"), (2, "bar"))
|
||||
testAll((1, "foo"), (2, "bar"))
|
||||
|
||||
// sortBy
|
||||
val words = "The quick brown fox jumped over the lazy dog".split(' ')
|
||||
val result = words.sortBy(x => (x.length, x.head))
|
||||
assert(result sameElements Array[String]("The", "dog", "fox", "the", "lazy", "over", "brown", "quick", "jumped"))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue