Skip to content

Commit b77ab7c

Browse files
committed
Remove operations from iterators. They are on views.
I've seen many projects, including Dotty itself where there is code similar to ```scala def average(xs: Iterator[Int]) = xs.sum/xs.size ``` It simply should not compile.
1 parent 5281688 commit b77ab7c

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

src/strawman/collections/CollectionStrawMan2.scala

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,22 @@ object CollectionStrawMan1 {
6565
protected def iter: Iterator[A]
6666
protected def fromIter(it: => Iterator[A]): Repr
6767
def partition(p: A => Boolean): (Repr, Repr) = {
68-
val (xs, ys) = iter.partition(p)
68+
val lookaheadTrue, lookaheadFalse = new ArrayBuffer[A]
69+
val xs = Iterator.Partition[A](this, p, lookaheadTrue, lookaheadFalse)
70+
val ys = Iterator.Partition[A](this, !p(_), lookaheadFalse, lookaheadTrue)
6971
(fromIter(xs), fromIter(ys))
7072
}
71-
def drop(n: Int): Repr = fromIter(iter.drop(n))
73+
def drop(n: Int): Repr = fromIter(Iterator.Drop(iter, n))
7274
}
7375

7476
/** Transforms returning same collection type constructor */
7577
trait PolyTransforms[A, C[X]] extends Any {
7678
protected def iter: Iterator[A]
7779
protected def fromIter[B](it: => Iterator[B]): C[B]
78-
def map[B](f: A => B): C[B] = fromIter(iter.map(f))
79-
def flatMap[B](f: A => CanIterate[B]): C[B] = fromIter(iter.flatMap(f(_)))
80-
def ++[B >: A](xs: CanIterate[B]): C[B] = fromIter(iter ++ xs)
81-
def zip[B](xs: CanIterate[B]): C[(A, B)] = fromIter(iter.zip(xs.iterator))
80+
def map[B](f: A => B): C[B] = fromIter(Iterator.Map(iter, f))
81+
def flatMap[B](f: A => CanIterate[B]): C[B] = fromIter(Iterator.FlatMap(iter, f))
82+
def ++[B >: A](xs: CanIterate[B]): C[B] = fromIter(Iterator.Concat(iter, xs))
83+
def zip[B](xs: CanIterate[B]): C[(A, B)] = fromIter(Iterator.Zip(iter, xs.iterator))
8284
}
8385

8486
/** Transforms that only apply to Seq */
@@ -285,30 +287,6 @@ object CollectionStrawMan1 {
285287
def hasNext: Boolean
286288
def next: A
287289
def iterator = this
288-
def foldLeft[B](z: B)(op: (B, A) => B): B =
289-
if (hasNext) foldLeft(op(z, next))(op) else z
290-
def foldRight[B](z: B)(op: (A, B) => B): B =
291-
if (hasNext) op(next, foldRight(z)(op)) else z
292-
def foreach(f: A => Unit): Unit =
293-
while (hasNext) f(next)
294-
def indexWhere(p: A => Boolean): Int = {
295-
var i = 0
296-
while (hasNext) {
297-
if (p(next)) return i
298-
i += 1
299-
}
300-
-1
301-
}
302-
def map[B](f: A => B): Iterator[B] = Iterator.Map(this, f)
303-
def flatMap[B](f: A => CanIterate[B]): Iterator[B] = Iterator.FlatMap(this, f)
304-
def ++[B >: A](xs: CanIterate[B]): Iterator[B] = Iterator.Concat(this, xs.iterator)
305-
def partition(p: A => Boolean): (Iterator[A], Iterator[A]) = {
306-
val lookaheadTrue, lookaheadFalse = new ArrayBuffer[A]
307-
(Iterator.Partition(this, p, lookaheadTrue, lookaheadFalse),
308-
Iterator.Partition[A](this, !p(_), lookaheadFalse, lookaheadTrue))
309-
}
310-
def drop(n: Int): Iterator[A] = Iterator.Drop(this, n)
311-
def zip[B](that: CanIterate[B]): Iterator[(A, B)] = Iterator.Zip(this, that.iterator)
312290
def reverse: Iterator[A] = {
313291
var elems: List[A] = Nil
314292
while (hasNext) elems = Cons(next, elems)

0 commit comments

Comments
 (0)