From 2eb186f67b196d933a0ae831bf8088a5b000ad12 Mon Sep 17 00:00:00 2001 From: Aleksandar Date: Sun, 6 May 2012 22:34:52 +0200 Subject: Add missing methods to GenTraversableLike. --- src/library/scala/collection/GenIterableLike.scala | 3 -- .../scala/collection/GenTraversableLike.scala | 43 +++++++++++++++++++--- src/library/scala/collection/TraversableLike.scala | 2 + .../collection/parallel/ParIterableLike.scala | 22 ++++++++++- 4 files changed, 60 insertions(+), 10 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/collection/GenIterableLike.scala b/src/library/scala/collection/GenIterableLike.scala index 79113ddaa7..290a3b26b5 100644 --- a/src/library/scala/collection/GenIterableLike.scala +++ b/src/library/scala/collection/GenIterableLike.scala @@ -141,7 +141,4 @@ trait GenIterableLike[+A, +Repr] extends Any with GenTraversableLike[A, Repr] { */ def zipAll[B, A1 >: A, That](that: GenIterable[B], thisElem: A1, thatElem: B)(implicit bf: CBF[Repr, (A1, B), That]): That - def isEmpty = iterator.isEmpty - - def head = iterator.next } diff --git a/src/library/scala/collection/GenTraversableLike.scala b/src/library/scala/collection/GenTraversableLike.scala index 903594b69d..df652f99d9 100644 --- a/src/library/scala/collection/GenTraversableLike.scala +++ b/src/library/scala/collection/GenTraversableLike.scala @@ -59,12 +59,24 @@ trait GenTraversableLike[+A, +Repr] extends Any with GenTraversableOnce[A] with def size: Int + /** Selects the first element of this $coll. + * $orderDependent + * @return the first element of this $coll. + * @throws `NoSuchElementException` if the $coll is empty. + */ def head: A - + + /** Optionally selects the first element. + * $orderDependent + * @return the first element of this $coll if it is nonempty, + * `None` if it is empty. + */ + def headOption: Option[A] + /** Tests whether this $coll can be repeatedly traversed. * @return `true` */ - final def isTraversableAgain = true + def isTraversableAgain: Boolean /** Selects all elements except the first. * $orderDependent @@ -72,11 +84,30 @@ trait GenTraversableLike[+A, +Repr] extends Any with GenTraversableOnce[A] with * except the first one. * @throws `UnsupportedOperationException` if the $coll is empty. */ - def tail: Repr = { - if (isEmpty) throw new UnsupportedOperationException("empty.tail") - drop(1) - } + def tail: Repr + /** Selects the last element. + * $orderDependent + * @return The last element of this $coll. + * @throws NoSuchElementException If the $coll is empty. + */ + def last: A + + /** Optionally selects the last element. + * $orderDependent + * @return the last element of this $coll$ if it is nonempty, + * `None` if it is empty. + */ + def lastOption: Option[A] + + /** Selects all elements except the last. + * $orderDependent + * @return a $coll consisting of all elements of this $coll + * except the last one. + * @throws `UnsupportedOperationException` if the $coll is empty. + */ + def init: Repr + /** Computes a prefix scan of the elements of the collection. * * Note: The neutral element `z` may be applied more than once. diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala index 3ca4bfeee9..3716a318d9 100644 --- a/src/library/scala/collection/TraversableLike.scala +++ b/src/library/scala/collection/TraversableLike.scala @@ -84,6 +84,8 @@ trait TraversableLike[+A, +Repr] extends Any */ def repr: Repr = this.asInstanceOf[Repr] + final def isTraversableAgain: Boolean = true + /** The underlying collection seen as an instance of `$Coll`. * By default this is implemented as the current collection object itself, * but this can be overridden. diff --git a/src/library/scala/collection/parallel/ParIterableLike.scala b/src/library/scala/collection/parallel/ParIterableLike.scala index 014b9b1a42..4cb229fc0f 100644 --- a/src/library/scala/collection/parallel/ParIterableLike.scala +++ b/src/library/scala/collection/parallel/ParIterableLike.scala @@ -178,10 +178,30 @@ self: ParIterableLike[T, Repr, Sequential] => def repr: Repr = this.asInstanceOf[Repr] + final def isTraversableAgain = true + def hasDefiniteSize = true + def isEmpty = size == 0 + def nonEmpty = size != 0 - + + def head = iterator.next + + def headOption = if (nonEmpty) Some(head) else None + + def tail = drop(1) + + def last = { + var lst = head + for (x <- this.seq) lst = x + lst + } + + def lastOption = if (nonEmpty) Some(last) else None + + def init = take(size - 1) + /** Creates a new parallel iterator used to traverse the elements of this parallel collection. * This iterator is more specific than the iterator of the returned by `iterator`, and augmented * with additional accessor and transformer methods. -- cgit v1.2.3