diff options
author | Martin Odersky <odersky@gmail.com> | 2016-07-27 10:06:35 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2016-07-27 10:07:12 +0200 |
commit | 568e48961e423a1dd1425c2c65bc129edba3f700 (patch) | |
tree | f0d74b3ab89eecf53f7fa9301111d2a3c64a30e8 | |
parent | 84466af8e3834e64bf350fe03976ee2176bb6916 (diff) | |
download | dotty-568e48961e423a1dd1425c2c65bc129edba3f700.tar.gz dotty-568e48961e423a1dd1425c2c65bc129edba3f700.tar.bz2 dotty-568e48961e423a1dd1425c2c65bc129edba3f700.zip |
Improve drop
By making LinearSeq an IterableLike, we can use tail-recursion
on drop.
-rw-r--r-- | src/strawman/collections/CollectionStrawMan6.scala | 15 | ||||
-rw-r--r-- | tests/run/colltest6/CollectionStrawMan6_1.scala | 13 |
2 files changed, 7 insertions, 21 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] { diff --git a/tests/run/colltest6/CollectionStrawMan6_1.scala b/tests/run/colltest6/CollectionStrawMan6_1.scala index b5776433e..1cd841336 100644 --- a/tests/run/colltest6/CollectionStrawMan6_1.scala +++ b/tests/run/colltest6/CollectionStrawMan6_1.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. |