diff options
author | Paul Phillips <paulp@improving.org> | 2011-03-08 15:40:53 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-03-08 15:40:53 +0000 |
commit | 783721e98a4f61fc0c72298811b15e99c058d5e6 (patch) | |
tree | cdc0786526c572cfb9198942d517fbfd36ed0ba5 /src/library/scala/collection/IterableLike.scala | |
parent | 8328a880b60ded33d5d49b88bdc75020b577eb27 (diff) | |
download | scala-783721e98a4f61fc0c72298811b15e99c058d5e6.tar.gz scala-783721e98a4f61fc0c72298811b15e99c058d5e6.tar.bz2 scala-783721e98a4f61fc0c72298811b15e99c058d5e6.zip |
An overhaul of slice and related implementation...
An overhaul of slice and related implementations (primarily that is
drop and take.) In the course of trying to get it working consistently
(mostly with respect to negative indices, which were dealt with
arbitrarily differently across the 25+ concrete implementations) I fixed
various bugs.
Closes #4288, no review.
Diffstat (limited to 'src/library/scala/collection/IterableLike.scala')
-rw-r--r-- | src/library/scala/collection/IterableLike.scala | 28 |
1 files changed, 4 insertions, 24 deletions
diff --git a/src/library/scala/collection/IterableLike.scala b/src/library/scala/collection/IterableLike.scala index 1b5bd4b822..16d7b0f66e 100644 --- a/src/library/scala/collection/IterableLike.scala +++ b/src/library/scala/collection/IterableLike.scala @@ -92,33 +92,13 @@ self => iterator.reduceRight(op) override /*TraversableLike*/ def toIterable: Iterable[A] = thisCollection - override /*TraversableLike*/ def head: A = - if (isEmpty) throw new NoSuchElementException - else iterator.next - - override /*TraversableLike*/ def take(n: Int): Repr = { - val b = newBuilder - b.sizeHintBounded(n, this) - var i = 0 - val it = iterator - while (i < n && it.hasNext) { - b += it.next - i += 1 - } - b.result - } + iterator.next override /*TraversableLike*/ def slice(from: Int, until: Int): Repr = { - val b = newBuilder - b.sizeHintBounded(until - from, this) - var i = from - val it = iterator drop from - while (i < until && it.hasNext) { - b += it.next - i += 1 - } - b.result + val lo = from max 0 + if (until <= lo) newBuilder.result + else newBuilder ++= (iterator drop lo take (until - lo)) result } override /*TraversableLike*/ def takeWhile(p: A => Boolean): Repr = { |