aboutsummaryrefslogtreecommitdiff
path: root/src/strawman/collections/CollectionStrawMan6.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2016-07-27 10:06:35 +0200
committerMartin Odersky <odersky@gmail.com>2016-07-27 10:07:12 +0200
commit568e48961e423a1dd1425c2c65bc129edba3f700 (patch)
treef0d74b3ab89eecf53f7fa9301111d2a3c64a30e8 /src/strawman/collections/CollectionStrawMan6.scala
parent84466af8e3834e64bf350fe03976ee2176bb6916 (diff)
downloaddotty-568e48961e423a1dd1425c2c65bc129edba3f700.tar.gz
dotty-568e48961e423a1dd1425c2c65bc129edba3f700.tar.bz2
dotty-568e48961e423a1dd1425c2c65bc129edba3f700.zip
Improve drop
By making LinearSeq an IterableLike, we can use tail-recursion on drop.
Diffstat (limited to 'src/strawman/collections/CollectionStrawMan6.scala')
-rw-r--r--src/strawman/collections/CollectionStrawMan6.scala15
1 files changed, 4 insertions, 11 deletions
diff --git a/src/strawman/collections/CollectionStrawMan6.scala b/src/strawman/collections/CollectionStrawMan6.scala
index b5776433e..6be02177b 100644
--- a/src/strawman/collections/CollectionStrawMan6.scala
+++ b/src/strawman/collections/CollectionStrawMan6.scala
@@ -138,7 +138,7 @@ object CollectionStrawMan6 extends LowPriority {
}
/** Base trait for linearly accessed sequences */
- trait LinearSeq[+A] extends Seq[A] { self =>
+ trait LinearSeq[+A] extends Seq[A] with SeqLike[A, LinearSeq] { self =>
def iterator = new Iterator[A] {
private[this] var current: Seq[A] = self
@@ -154,15 +154,8 @@ object CollectionStrawMan6 extends LowPriority {
def length: Int = if (isEmpty) 0 else 1 + tail.length
/** Optimized version of `drop` that avoids copying */
- final override def drop(n: Int) = {
- var current: Seq[A] = this
- var i = n
- while (i > 0) {
- current = current.tail
- i -= 1
- }
- current
- }
+ @tailrec final override def drop(n: Int) =
+ if (n <= 0) this else tail.drop(n - 1)
}
/** Base trait for strict collections that can be built using a builder.
@@ -598,7 +591,7 @@ object CollectionStrawMan6 extends LowPriority {
case None => "Empty"
case Some((hd, tl)) => s"$hd #:: $tl"
}
- else "?"
+ else "LazyList(?)"
}
object LazyList extends IterableFactory[LazyList] {