Skip to content

Commit

Permalink
Add missing methods to GenTraversableLike.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandar committed May 6, 2012
1 parent f146d58 commit 2eb186f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 10 deletions.
3 changes: 0 additions & 3 deletions src/library/scala/collection/GenIterableLike.scala
Expand Up @@ -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 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
} }
43 changes: 37 additions & 6 deletions src/library/scala/collection/GenTraversableLike.scala
Expand Up @@ -59,24 +59,55 @@ trait GenTraversableLike[+A, +Repr] extends Any with GenTraversableOnce[A] with


def size: Int 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 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. /** Tests whether this $coll can be repeatedly traversed.
* @return `true` * @return `true`
*/ */
final def isTraversableAgain = true def isTraversableAgain: Boolean


/** Selects all elements except the first. /** Selects all elements except the first.
* $orderDependent * $orderDependent
* @return a $coll consisting of all elements of this $coll * @return a $coll consisting of all elements of this $coll
* except the first one. * except the first one.
* @throws `UnsupportedOperationException` if the $coll is empty. * @throws `UnsupportedOperationException` if the $coll is empty.
*/ */
def tail: Repr = { def tail: Repr
if (isEmpty) throw new UnsupportedOperationException("empty.tail")
drop(1)
}


/** 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. /** Computes a prefix scan of the elements of the collection.
* *
* Note: The neutral element `z` may be applied more than once. * Note: The neutral element `z` may be applied more than once.
Expand Down
2 changes: 2 additions & 0 deletions src/library/scala/collection/TraversableLike.scala
Expand Up @@ -84,6 +84,8 @@ trait TraversableLike[+A, +Repr] extends Any
*/ */
def repr: Repr = this.asInstanceOf[Repr] def repr: Repr = this.asInstanceOf[Repr]


final def isTraversableAgain: Boolean = true

/** The underlying collection seen as an instance of `$Coll`. /** The underlying collection seen as an instance of `$Coll`.
* By default this is implemented as the current collection object itself, * By default this is implemented as the current collection object itself,
* but this can be overridden. * but this can be overridden.
Expand Down
22 changes: 21 additions & 1 deletion src/library/scala/collection/parallel/ParIterableLike.scala
Expand Up @@ -178,10 +178,30 @@ self: ParIterableLike[T, Repr, Sequential] =>


def repr: Repr = this.asInstanceOf[Repr] def repr: Repr = this.asInstanceOf[Repr]


final def isTraversableAgain = true

def hasDefiniteSize = true def hasDefiniteSize = true


def isEmpty = size == 0

def nonEmpty = 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. /** 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 * This iterator is more specific than the iterator of the returned by `iterator`, and augmented
* with additional accessor and transformer methods. * with additional accessor and transformer methods.
Expand Down
20 changes: 20 additions & 0 deletions test/files/pos/gen-traversable-methods.scala
@@ -0,0 +1,20 @@



import collection._



object Test {

def main(args: Array[String]) {
val gen: GenTraversable[Int] = List(1, 2, 3)
gen.head
gen.headOption
gen.tail
gen.last
gen.lastOption
gen.init
}

}

0 comments on commit 2eb186f

Please sign in to comment.