diff options
author | Paul Phillips <paulp@improving.org> | 2009-10-22 16:26:47 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-10-22 16:26:47 +0000 |
commit | a2eab2215a7671e8abd8189ccc59388dffc1f3de (patch) | |
tree | d4d2e239c0b628d4365cfe3441551c8fa88ca809 | |
parent | c23174011d44ddf9b405feccf5823d55a6178e06 (diff) | |
download | scala-a2eab2215a7671e8abd8189ccc59388dffc1f3de.tar.gz scala-a2eab2215a7671e8abd8189ccc59388dffc1f3de.tar.bz2 scala-a2eab2215a7671e8abd8189ccc59388dffc1f3de.zip |
Added (a variation on) jorge ortiz's sortBy to ...
Added (a variation on) jorge ortiz's sortBy to SeqLike, added docs and
test case. Added map to Ordering.
-rw-r--r-- | src/library/scala/Ordering.scala | 5 | ||||
-rw-r--r-- | src/library/scala/collection/SeqLike.scala | 27 | ||||
-rw-r--r-- | test/files/run/OrderingTest.scala | 5 |
3 files changed, 33 insertions, 4 deletions
diff --git a/src/library/scala/Ordering.scala b/src/library/scala/Ordering.scala index 8b4db3f549..4a90a03dfb 100644 --- a/src/library/scala/Ordering.scala +++ b/src/library/scala/Ordering.scala @@ -92,6 +92,11 @@ trait Ordering[T] extends Comparator[T] with PartialOrdering[T] { 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) def <=(rhs: T) = lteq(lhs, rhs) diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala index 9f80386b2a..a551a0b961 100644 --- a/src/library/scala/collection/SeqLike.scala +++ b/src/library/scala/collection/SeqLike.scala @@ -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") @@ -561,6 +562,24 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self => 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. * diff --git a/test/files/run/OrderingTest.scala b/test/files/run/OrderingTest.scala index 49953b22d0..fee15fc2f9 100644 --- a/test/files/run/OrderingTest.scala +++ b/test/files/run/OrderingTest.scala @@ -28,4 +28,9 @@ object Test extends Application { testAll[Iterable[Int]](List(1, 2), List(2)); testAll((1, "bar"), (1, "foo")) 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")) } |