summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/TraversableOnce.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-11-29 23:00:07 +0000
committerPaul Phillips <paulp@improving.org>2010-11-29 23:00:07 +0000
commit66a92814a61c62149a49335f65f4189763b43296 (patch)
tree981508efdd7a29daa12f3e121d9608e441f26ece /src/library/scala/collection/TraversableOnce.scala
parent4ec7f11a799444c3758e94b3fdf9fa5c26330577 (diff)
downloadscala-66a92814a61c62149a49335f65f4189763b43296.tar.gz
scala-66a92814a61c62149a49335f65f4189763b43296.tar.bz2
scala-66a92814a61c62149a49335f65f4189763b43296.zip
The initial implementation of TraversableOnce c...
The initial implementation of TraversableOnce could not supply concrete methods or even signatures for map and flatMap because they have different signatures in Iterator and TraversableLike. But we can take another approach which works out as nicely: 1) Create implicits which install those methods and flatten on TraversableOnce instances. 2) Generalize the signatures of flatten and flatMap to work with A => TraversableOnce[B] instead of A => Traversable[B]. And voila, you can mix and match Iterators and Traversables in a for comprehension, map, flatMap, and flatten, without the tedious process of sprinkling .iterator or .toList around to appease the compiler. No review.
Diffstat (limited to 'src/library/scala/collection/TraversableOnce.scala')
-rw-r--r--src/library/scala/collection/TraversableOnce.scala42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/library/scala/collection/TraversableOnce.scala b/src/library/scala/collection/TraversableOnce.scala
index 1011108d8e..179051553e 100644
--- a/src/library/scala/collection/TraversableOnce.scala
+++ b/src/library/scala/collection/TraversableOnce.scala
@@ -591,3 +591,45 @@ trait TraversableOnce[+A] {
*/
def addString(b: StringBuilder): StringBuilder = addString(b, "")
}
+
+object TraversableOnce {
+ implicit def traversableOnceCanBuildFrom[T]: TraversableOnceCanBuildFrom[T] =
+ new TraversableOnceCanBuildFrom[T]
+
+ implicit def wrapTraversableOnce[A](trav: TraversableOnce[A]): TraversableOnceMonadOps[A] =
+ new TraversableOnceMonadOps(trav)
+
+ implicit def flattenTraversableOnce[A](travs: TraversableOnce[TraversableOnce[A]]): TraversableOnceFlattenOps[A] =
+ new TraversableOnceFlattenOps[A](travs)
+
+ /** With the advent of TraversableOnce, it can be useful to have a builder which
+ * operates on Iterators so they can be treated uniformly along with the collections.
+ * See scala.util.Random.shuffle for an example.
+ */
+ class TraversableOnceCanBuildFrom[A] extends generic.CanBuildFrom[TraversableOnce[A], A, TraversableOnce[A]] {
+ def newIterator = new ArrayBuffer[A] mapResult (_.iterator)
+
+ /** Creates a new builder on request of a collection.
+ * @param from the collection requesting the builder to be created.
+ * @return the result of invoking the `genericBuilder` method on `from`.
+ */
+ def apply(from: TraversableOnce[A]) = newIterator
+
+ /** Creates a new builder from scratch
+ * @return the result of invoking the `newBuilder` method of this factory.
+ */
+ def apply() = newIterator
+ }
+
+ class TraversableOnceFlattenOps[A](travs: TraversableOnce[TraversableOnce[A]]) {
+ def flatten: Iterator[A] = travs.foldLeft(Iterator.empty: Iterator[A])(_ ++ _)
+ }
+
+ class TraversableOnceMonadOps[+A](trav: TraversableOnce[A]) {
+ def map[B](f: A => B): TraversableOnce[B] = trav.toIterator map f
+ def flatMap[B](f: A => TraversableOnce[B]): TraversableOnce[B] = trav.toIterator flatMap f
+ def filter(p: A => Boolean): TraversableOnce[A] = trav.toIterator filter p
+ def withFilter(p: A => Boolean) = filter(p)
+ }
+}
+