Skip to content

Commit

Permalink
Tweaks to conform with API set out in scala#818.
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Oct 6, 2015
1 parent 1521121 commit d42f4fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
11 changes: 4 additions & 7 deletions src/strawman/collections/CollectionStrawMan1.scala
Expand Up @@ -20,9 +20,6 @@ object CollectionStrawMan1 {
def iterator: Iterator[A]
}

/** Iterator guaranteed to be usable multiple times */
trait HasIterator[+A] extends CanIterate[A]

/** Base trait for instances that can construct a collection from an iterator */
trait FromIterator[+C[X] <: Iterable[X]] {
def fromIterator[B](it: Iterator[B]): C[B]
Expand All @@ -35,7 +32,7 @@ object CollectionStrawMan1 {
}

/** Base trait for generic collections */
trait Iterable[+A] extends HasIterator[A] with FromIterator[Iterable]
trait Iterable[+A] extends CanIterate[A] with FromIterator[Iterable]

/** Base trait for sequence collections */
trait Seq[+A] extends Iterable[A] with FromIterator[Seq] {
Expand All @@ -55,7 +52,7 @@ object CollectionStrawMan1 {
def isEmpty: Boolean = !iterator.hasNext
def head: A = iterator.next
def view: View[A] = new View(iterator)
def collect[C[X] <: Iterable[X]](fi: FromIterator[C]): C[A] = fi.fromIterator(iterator)
def collectAs[C[X] <: Iterable[X]](fi: FromIterator[C]): C[A] = fi.fromIterator(iterator)
}

/** Transforms returning same collection type */
Expand Down Expand Up @@ -213,13 +210,13 @@ object CollectionStrawMan1 {
}

/** Concrete collection type: View */
class View[+A](it: => Iterator[A]) extends HasIterator[A] {
class View[+A](it: => Iterator[A]) extends CanIterate[A] {
def iterator = it
}

implicit class ViewOps[A](val v: View[A]) extends AnyVal with Ops[A] {
def iterator = v.iterator
def cache = collect(ArrayBuffer).view
def cache = collectAs(ArrayBuffer).view
}

implicit class ViewMonoTransforms[A](val v: View[A])
Expand Down
28 changes: 14 additions & 14 deletions tests/run/CollectionTests.scala
Expand Up @@ -14,7 +14,7 @@ object Test {
val y3: Int = x3
val x4 = xs.head
val y4: Int = x4
val x5 = xs.collect(List)
val x5 = xs.collectAs(List)
val y5: List[Int] = x5
val (xs6, xs7) = xs.partition(_ % 2 == 0)
val ys6: Seq[Int] = xs6
Expand Down Expand Up @@ -65,7 +65,7 @@ object Test {
val y3: Int = x3
val x4 = xs.head
val y4: Int = x4
val x5 = xs.collect(List)
val x5 = xs.collectAs(List)
val y5: List[Int] = x5
val (xs6, xs7) = xs.partition(_ % 2 == 0)
val ys6: View[Int] = xs6
Expand All @@ -92,16 +92,16 @@ object Test {
println(x3)
println(x4)
println(x5)
println(xs6.collect(List))
println(xs7.collect(List))
println(xs8.collect(List))
println(xs9.collect(List))
println(xs10.collect(List))
println(xs11.collect(List))
println(xs12.collect(List))
println(xs13.collect(List))
println(xs14.collect(List))
println(xs15.collect(List))
println(xs6.collectAs(List))
println(xs7.collectAs(List))
println(xs8.collectAs(List))
println(xs9.collectAs(List))
println(xs10.collectAs(List))
println(xs11.collectAs(List))
println(xs12.collectAs(List))
println(xs13.collectAs(List))
println(xs14.collectAs(List))
println(xs15.collectAs(List))
}

def stringOps(xs: String) = {
Expand All @@ -113,7 +113,7 @@ object Test {
val y3: Int = x3
val x4 = xs.head
val y4: Int = x4
val x5 = xs.collect(List)
val x5 = xs.collectAs(List)
val y5: List[Char] = x5
val (xs6, xs7) = xs.partition(_ % 2 == 0)
val ys6: String = xs6
Expand Down Expand Up @@ -160,7 +160,7 @@ object Test {

def main(args: Array[String]) = {
val ints = Cons(1, Cons(2, Cons(3, Nil)))
val intsBuf = ints.collect(ArrayBuffer)
val intsBuf = ints.collectAs(ArrayBuffer)
val intsView = ints.view
seqOps(ints)
seqOps(intsBuf)
Expand Down

0 comments on commit d42f4fe

Please sign in to comment.