@@ -280,39 +280,39 @@ implementations of `filter` and `map`:
280
280
trait IterableOps [+ A , + CC [_], + C ] {
281
281
282
282
def filter (pred : A => Boolean ): C =
283
- fromSpecificIterable (new View .Filter (this , pred))
283
+ fromSpecific (new View .Filter (this , pred))
284
284
285
285
def map [B ](f : A => B ): CC [B ] =
286
- fromIterable (new View .Map (this , f))
286
+ from (new View .Map (this , f))
287
287
288
- protected def fromSpecificIterable (coll : Iterable [A ]): C
289
- protected def fromIterable [E ](it : Iterable [E ]): CC [E ]
288
+ protected def fromSpecific (coll : IterableOnce [A ]): C
289
+ protected def from [E ](it : IterableOnce [E ]): CC [E ]
290
290
}
291
291
~~~
292
292
293
293
Let’s detail the implementation of ` filter ` , step by step:
294
294
295
295
- the instantiation of ` View.Filter ` creates a (non-strict) ` View ` that filters the elements
296
296
of the underlying collection ;
297
- - the call to ` fromSpecificIterable ` turns the ` View ` into a concrete
298
- collection ` C ` . The implementation of ` fromSpecificIterable ` is left to
297
+ - the call to ` fromSpecific ` turns the ` View ` into a concrete
298
+ collection ` C ` . The implementation of ` fromSpecific ` is left to
299
299
concrete collections: they can decide to evaluate in a strict or non-strict way
300
300
the elements resulting from the operation.
301
301
302
302
The implementation of ` map ` is similar, excepted that instead of using
303
- ` fromSpecificIterable ` it uses ` fromIterable ` which takes as parameter an
303
+ ` fromSpecific ` it uses ` from ` which takes as parameter an
304
304
iterable whose element type ` E ` is arbitrary.
305
305
306
- Actually, ` fromIterable ` is not abstract in ` IterableOps ` : it delegates to an
307
- ` iterableFactory ` member (which is abstract) :
306
+ Actually, the ` from ` operation is not defined directly in ` IterableOps ` but is accessed via
307
+ an (abstract) ` iterableFactory ` member:
308
308
309
309
~~~ scala
310
310
trait IterableOps [+ A , + CC [_], + C ] {
311
311
312
- protected def fromIterable [E ](it : Iterable [E ]): CC [E ] =
313
- iterableFactory.from(it)
314
-
315
312
def iterableFactory : IterableFactory [CC ]
313
+
314
+ def map [B ](f : A => B ): CC [B ] =
315
+ iterableFactory.from(new View .Map (this , f))
316
316
317
317
}
318
318
~~~
@@ -336,12 +336,9 @@ trait MapOps[K, +V, +CC[_, _], +C]
336
336
extends IterableOps [(K , V ), Iterable , C ] {
337
337
338
338
def map [K2 , V2 ](f : ((K , V )) => (K2 , V2 )): CC [K2 , V2 ] =
339
- mapFromIterable(new View .Map (this , f))
340
-
341
- // Similar to fromIterable, but returns a Map collection type
342
- protected def mapFromIterable [K2 , V2 ](it : Iterable [(K2 , V2 )]): CC [K2 , V2 ] =
343
- mapFactory.from(it)
339
+ mapFactory.from(new View .Map (this , f))
344
340
341
+ // Similar to iterableFactory, but for Map collection types
345
342
def mapFactory : MapFactory [CC ]
346
343
347
344
}
@@ -387,7 +384,7 @@ type of elements. The following code shows the relevant parts of `IterableOps` a
387
384
~~~ scala
388
385
trait IterableOps [+ A , + CC [_], + C ] {
389
386
def iterableFactory : IterableFactory [CC ]
390
- protected def fromSpecificIterable (coll : Iterable [A ]): C
387
+ protected def fromSpecific (coll : IterableOnce [A ]): C
391
388
protected def newSpecificBuilder : Builder [A , C ]
392
389
}
393
390
@@ -428,3 +425,10 @@ This document explains that:
428
425
429
426
You now have all the required knowledge to implement
430
427
[ custom collection types] ({{ site.baseurl }}/overviews/core/custom-collections.html).
428
+
429
+ ### Acknowledgement ###
430
+
431
+ This page contains material adapted from the book
432
+ [ Programming in Scala] ( http://www.artima.com/shop/programming_in_scala ) by
433
+ Odersky, Spoon and Venners. We thank Artima for graciously agreeing to its
434
+ publication.
0 commit comments