summaryrefslogtreecommitdiff
path: root/test/files/pos/spec-List.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2009-09-25 16:20:13 +0000
committerMartin Odersky <odersky@gmail.com>2009-09-25 16:20:13 +0000
commit4a727f3b01d0fa27ef51f7dba472116e021e3445 (patch)
treec9ab55ea7fe6051455271b23e9fbfc2f313015c0 /test/files/pos/spec-List.scala
parente31f18094dfba97c80871869a037172ff2c9c1c2 (diff)
downloadscala-4a727f3b01d0fa27ef51f7dba472116e021e3445.tar.gz
scala-4a727f3b01d0fa27ef51f7dba472116e021e3445.tar.bz2
scala-4a727f3b01d0fa27ef51f7dba472116e021e3445.zip
Collections refactoring.
Diffstat (limited to 'test/files/pos/spec-List.scala')
-rw-r--r--test/files/pos/spec-List.scala34
1 files changed, 22 insertions, 12 deletions
diff --git a/test/files/pos/spec-List.scala b/test/files/pos/spec-List.scala
index 7dce0305a9..7cd6a03f6c 100644
--- a/test/files/pos/spec-List.scala
+++ b/test/files/pos/spec-List.scala
@@ -7,10 +7,12 @@
\* */
-package scala.collection.immutable
+package scala.collection
+package immutable
-import scala.collection.mutable.ListBuffer
-import scala.collection.generic._
+import generic._
+import mutable.{Builder, ListBuffer}
+import annotation.tailrec
/** A class representing an ordered collection of elements of type
* <code>a</code>. This class comes with two implementing case
@@ -23,11 +25,11 @@ import scala.collection.generic._
*/
sealed abstract class List[@specialized +A] extends LinearSequence[A]
with Product
- with TraversableClass[A, List]
- with LinearSequenceTemplate[A, List[A]] {
- override def companion: Companion[List] = List
+ with GenericTraversableTemplate[A, List]
+ with LinearSequenceLike[A, List[A]] {
+ override def companion: GenericCompanion[List] = List
- import collection.{Iterable, Traversable, Sequence, Vector}
+ import scala.collection.{Iterable, Traversable, Sequence, Vector}
/** Returns true if the list does not contain any elements.
* @return <code>true</code>, iff the list is empty.
@@ -100,6 +102,7 @@ sealed abstract class List[@specialized +A] extends LinearSequence[A]
* @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)
@@ -136,7 +139,7 @@ sealed abstract class List[@specialized +A] extends LinearSequence[A]
loop(this)
}
- // Overridden methods from IterableTemplate or overloaded variants of such methods
+ // Overridden methods from IterableLike or overloaded variants of such methods
/** Create a new list which contains all elements of this list
* followed by all elements of Traversable `that'
@@ -212,6 +215,7 @@ sealed abstract class List[@specialized +A] extends LinearSequence[A]
* @return the suffix of length <code>n</code> of the list
*/
override def takeRight(n: Int): List[A] = {
+ @tailrec
def loop(lead: List[A], lag: List[A]): List[A] = lead match {
case Nil => lag
case _ :: tail => loop(tail, lag.tail)
@@ -264,9 +268,14 @@ sealed abstract class List[@specialized +A] extends LinearSequence[A]
* @return the longest suffix of the list whose first element
* does not satisfy the predicate <code>p</code>.
*/
- override def dropWhile(p: A => Boolean): List[A] =
- if (isEmpty || !p(head)) this
- else tail dropWhile p
+ override def dropWhile(p: A => Boolean): List[A] = {
+ @tailrec
+ def loop(xs: List[A]): List[A] =
+ if (xs.isEmpty || !p(xs.head)) xs
+ else loop(xs.tail)
+
+ loop(this)
+ }
/** Returns the longest prefix of the list whose elements all satisfy
* the given predicate, and the rest of the list.
@@ -346,7 +355,7 @@ sealed abstract class List[@specialized +A] extends LinearSequence[A]
* <code>&lt;(e1: a, e2: a) =&gt; Boolean</code>,
* which should be true iff <code>e1</code> is smaller than
* <code>e2</code>.
- * !!! todo: move sorting to IterableTemplate
+ * !!! todo: move sorting to IterableLike
* </p>
*
* @param lt the comparison function
@@ -435,6 +444,7 @@ case object Nil extends List[Nothing] {
throw new NoSuchElementException("head of empty list")
override def tail: List[Nothing] =
throw new NoSuchElementException("tail of empty list")
+ // Removal of equals method here might lead to an infinite recusion similar to IntMap.equals.
override def equals(that: Any) = that match {
case that1: Sequence[_] => that1.isEmpty
case _ => false