diff options
author | Paul Phillips <paulp@improving.org> | 2009-11-11 00:56:02 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-11-11 00:56:02 +0000 |
commit | 1e9a86e70122babbba0d248b930b78584871fc87 (patch) | |
tree | a3ea9bfb92f97b7935adfb6adc60d0f9af39f3d8 | |
parent | f0664e90356e92cc9935d2151e699aafb5801476 (diff) | |
download | scala-1e9a86e70122babbba0d248b930b78584871fc87.tar.gz scala-1e9a86e70122babbba0d248b930b78584871fc87.tar.bz2 scala-1e9a86e70122babbba0d248b930b78584871fc87.zip |
Promotes reverseMap out of List into SeqLike as...
Promotes reverseMap out of List into SeqLike as discussed on
scala-internals.
-rw-r--r-- | src/library/scala/collection/SeqLike.scala | 23 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/List.scala | 16 |
2 files changed, 19 insertions, 20 deletions
diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala index 708764e958..749aeacd73 100644 --- a/src/library/scala/collection/SeqLike.scala +++ b/src/library/scala/collection/SeqLike.scala @@ -259,10 +259,6 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self => /** A sequence of type <code>C</code> consisting of all elements of * this sequence in reverse order. - * @note the operation is implemented by building a new sequence - * from <code>this(length - 1), ..., this(0)</code> - * If random access is inefficient for the given sequence implementation, - * this operation should be overridden. */ def reverse: Repr = { var xs: List[A] = List() @@ -274,6 +270,25 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self => b.result } + /** Apply a function to all the elements of the sequence, and return the + * reversed sequence of results. This is equivalent to a call to <code>reverse</code> + * followed by a call to <code>map</code>, but more efficient. + * + * @param f the function to apply to each elements. + * @return the reversed seq of results. + */ + def reverseMap[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { + var xs: List[A] = List() + for (x <- this) + xs = x :: xs + + val b = bf(repr) + for (x <- xs) + b += f(x) + + b.result + } + /** The elements of this sequence in reversed order */ def reverseIterator: Iterator[A] = toCollection(reverse).iterator diff --git a/src/library/scala/collection/immutable/List.scala b/src/library/scala/collection/immutable/List.scala index 1412c8487a..94732d7ad5 100644 --- a/src/library/scala/collection/immutable/List.scala +++ b/src/library/scala/collection/immutable/List.scala @@ -97,22 +97,6 @@ sealed abstract class List[+A] extends LinearSeq[A] these } - /** Apply a function to all the elements of the list, and return the - * reversed list of results. This is equivalent to a call to <code>map</code> - * followed by a call to <code>reverse</code>, but more efficient. - * !!! should we deprecate this? Why have reverseMap, but not filterMap or reverseFilter, say? - * @param f the function to apply to each elements. - * @return the reversed list of results. - */ - def reverseMap[B](f: A => B): List[B] = { - @tailrec - def loop(l: List[A], res: List[B]): List[B] = l match { - case Nil => res - case head :: tail => loop(tail, f(head) :: res) - } - loop(this, Nil) - } - /** Like xs map f, but returns <code>xs</code> unchanged if function * <code>f</code> maps all elements to themselves (wrt ==). * @note Unlike `map`, `mapConserve` is not tail-recursive. |