diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2011-04-13 16:31:42 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2011-04-13 16:31:42 +0000 |
commit | 3de96153e5bfbde16dcc89bfbd71ff6e8cf1f6c6 (patch) | |
tree | 2794a7bd176b315a9f4bdc3f5ef5553254b7dd47 /src/library/scala/collection/IterableLike.scala | |
parent | 9b5cb18dbd2d3e87def5da47ae76adb2e776487e (diff) | |
download | scala-3de96153e5bfbde16dcc89bfbd71ff6e8cf1f6c6.tar.gz scala-3de96153e5bfbde16dcc89bfbd71ff6e8cf1f6c6.tar.bz2 scala-3de96153e5bfbde16dcc89bfbd71ff6e8cf1f6c6.zip |
Refactoring the collections api to support diff...
Refactoring the collections api to support differentiation between
referring to a sequential collection and a parallel collection, and to
support referring to both types of collections.
New set of traits Gen* are now superclasses of both their * and Par* subclasses. For example, GenIterable is a superclass of both Iterable and ParIterable. Iterable and ParIterable are not in a subclassing relation. The new class hierarchy is illustrated below (simplified, not all relations and classes are shown):
TraversableOnce --> GenTraversableOnce
^ ^
| |
Traversable --> GenTraversable
^ ^
| |
Iterable --> GenIterable <-- ParIterable
^ ^ ^
| | |
Seq --> GenSeq <-- ParSeq
(the *Like, *View and *ViewLike traits have a similar hierarchy)
General views extract common view functionality from parallel and
sequential collections.
This design also allows for more flexible extensions to the collections
framework. It also allows slowly factoring out common functionality up
into Gen* traits.
From now on, it is possible to write this:
import collection._
val p = parallel.ParSeq(1, 2, 3)
val g: GenSeq[Int] = p // meaning a General Sequence
val s = g.seq // type of s is Seq[Int]
for (elem <- g) {
// do something without guarantees on sequentiality of foreach
// this foreach may be executed in parallel
}
for (elem <- s) {
// do something with a guarantee that foreach is executed in order, sequentially
}
for (elem <- p) {
// do something concurrently, in parallel
}
This also means that some signatures had to be changed. For example,
method `flatMap` now takes `A => GenTraversableOnce[B]`, and `zip` takes
a `GenIterable[B]`.
Also, there are mutable & immutable Gen* trait variants. They have
generic companion functionality.
Diffstat (limited to 'src/library/scala/collection/IterableLike.scala')
-rw-r--r-- | src/library/scala/collection/IterableLike.scala | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/library/scala/collection/IterableLike.scala b/src/library/scala/collection/IterableLike.scala index 062b3c207f..4d10f31920 100644 --- a/src/library/scala/collection/IterableLike.scala +++ b/src/library/scala/collection/IterableLike.scala @@ -55,7 +55,7 @@ import annotation.unchecked.uncheckedVariance * result class `That` from the current representation type `Repr` * and the new element type `(A1, B)`. */ -trait IterableLike[+A, +Repr] extends Equals with TraversableLike[A, Repr] { +trait IterableLike[+A, +Repr] extends Equals with TraversableLike[A, Repr] with GenIterableLike[A, Repr] { self => override protected[this] def thisCollection: Iterable[A] = this.asInstanceOf[Iterable[A]] @@ -218,7 +218,7 @@ self => * corresponding elements of this $coll and `that`. The length * of the returned collection is the minimum of the lengths of this $coll and `that`. */ - def zip[A1 >: A, B, That](that: Iterable[B])(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = { + def zip[A1 >: A, B, That](that: GenIterable[B])(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = { val b = bf(repr) val these = this.iterator val those = that.iterator @@ -255,7 +255,7 @@ self => * If this $coll is shorter than `that`, `thisElem` values are used to pad the result. * If `that` is shorter than this $coll, `thatElem` values are used to pad the result. */ - def zipAll[B, A1 >: A, That](that: Iterable[B], thisElem: A1, thatElem: B)(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = { + def zipAll[B, A1 >: A, That](that: GenIterable[B], thisElem: A1, thatElem: B)(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = { val b = bf(repr) val these = this.iterator val those = that.iterator @@ -317,7 +317,7 @@ self => * @param that the collection to compare with. * @return `true`, if both collections contain the same elements in the same order, `false` otherwise. */ - def sameElements[B >: A](that: Iterable[B]): Boolean = { + def sameElements[B >: A](that: GenIterable[B]): Boolean = { val these = this.iterator val those = that.iterator while (these.hasNext && those.hasNext) |