@@ -65,20 +65,22 @@ object CollectionStrawMan1 {
65
65
protected def iter : Iterator [A ]
66
66
protected def fromIter (it : => Iterator [A ]): Repr
67
67
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)
69
71
(fromIter(xs), fromIter(ys))
70
72
}
71
- def drop (n : Int ): Repr = fromIter(iter.drop( n))
73
+ def drop (n : Int ): Repr = fromIter(Iterator . Drop (iter, n))
72
74
}
73
75
74
76
/** Transforms returning same collection type constructor */
75
77
trait PolyTransforms [A , C [X ]] extends Any {
76
78
protected def iter : Iterator [A ]
77
79
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))
82
84
}
83
85
84
86
/** Transforms that only apply to Seq */
@@ -285,30 +287,6 @@ object CollectionStrawMan1 {
285
287
def hasNext : Boolean
286
288
def next : A
287
289
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)
312
290
def reverse : Iterator [A ] = {
313
291
var elems : List [A ] = Nil
314
292
while (hasNext) elems = Cons (next, elems)
0 commit comments