scala.collection.generic
Members list
Type members
Classlikes
Mix-in trait to enable DefaultSerializationProxy for the standard collection types. Depending on the type it is mixed into, it will dynamically choose iterableFactory, mapFactory, sortedIterableFactory or sortedMapFactory for deserialization into the respective CC type. Override writeReplace or implement it directly without using this trait if you need a non-standard factory or if you want to use a different serialization scheme.
Mix-in trait to enable DefaultSerializationProxy for the standard collection types. Depending on the type it is mixed into, it will dynamically choose iterableFactory, mapFactory, sortedIterableFactory or sortedMapFactory for deserialization into the respective CC type. Override writeReplace or implement it directly without using this trait if you need a non-standard factory or if you want to use a different serialization scheme.
Attributes
- Source
- DefaultSerializationProxy.scala
- Supertypes
- Known subtypes
-
class HashSet[A]class List[A]class ::[A]object Nilclass ListSet[A]class Queue[A]class TreeSet[A]class Vector[A]class ArrayBuffer[A]class ArrayDeque[A]class Queue[A]class Stack[A]class LinkedHashSet[A]class ListBuffer[A]class TreeSet[A]class UnrolledBuffer[T]Show all
- Self type
-
The default serialization proxy for collection implementations.
The default serialization proxy for collection implementations.
This class is final and requires an extra Factory object rather than leaving the details of creating a Builder to an abstract method that could be implemented by a subclass. This is necessary because the factory is needed for deserializing this class's private state, which happens before any subclass fields would be deserialized. Any additional state required to create the proper Builder needs to be captured by the factory.
Attributes
- Source
- DefaultSerializationProxy.scala
- Supertypes
A trait which can be used to avoid code duplication when defining extension methods that should be applicable both to existing Scala collections (i.e., types extending Iterable) as well as other (potentially user-defined) types that could be converted to a Scala collection type. This trait makes it possible to uniformly treat Scala collections and types that can be implicitly converted to a collection type. For example, one can provide extension methods that work both on collection types and on Strings. (Strings do not extend Iterable, but can be converted to Iterable.)
A trait which can be used to avoid code duplication when defining extension methods that should be applicable both to existing Scala collections (i.e., types extending Iterable) as well as other (potentially user-defined) types that could be converted to a Scala collection type. This trait makes it possible to uniformly treat Scala collections and types that can be implicitly converted to a collection type. For example, one can provide extension methods that work both on collection types and on Strings. (Strings do not extend Iterable, but can be converted to Iterable.)
IsIterable provides three members:
- type member
A, which represents the element type of the targetIterable[A] - type member
C, which represents the type returned by transformation operations that preserve the collection’s element type - method
apply, which provides a way to convert between the type we wish to add extension methods to,Repr, andIterableOps[A, Iterable, C].
Usage
One must provide IsIterable as an implicit parameter of the extension method. Its usage is shown below. Our objective in the following example is to provide a generic extension method mapReduce for any type that extends or can be converted to Iterable, such as String.
import scala.collection.generic.IsIterable
extension [Repr, I <: IsIterable[Repr]](coll: Repr)(using it: I)
def mapReduce[B](mapper: it.A => B)(reducer: (B, B) => B): B = {
val iter = it(coll).iterator
var res = mapper(iter.next())
while (iter.hasNext)
res = reducer(res, mapper(iter.next()))
res
}
// See it in action!
List(1, 2, 3).mapReduce(_ * 2)(_ + _) // res0: Int = 12
"Yeah, well, you know, that's just, like, your opinion, man.".mapReduce(x => 1)(_ + _) // res1: Int = 59
The extension method takes a receiver coll of type Repr, where Repr typically represents the collection type, and an argument it of a subtype of IsIterable[Repr].
The body of the method starts by converting the coll argument to an IterableOps in order to call the iterator method on it. The rest of the implementation is straightforward.
The mapReduce extension method is available on any type Repr for which there is an implicit IsIterable[Repr] instance. The companion object for IsIterable provides an instance for types that are already an IterableOps.
Implementing IsIterable for New Types
For a custom type, one need only provide an implicit value of type IsIterable that specifies the element type, the collection type, and an implementation of apply that converts the collection to an IterableOps.
Below is an example implementation of the IsIterable trait where the Repr type is Range. In practice, IsIterable[Range] is already provided by the implicit value for any IterableOps, as for List in the previous example. Similarly, the instance for String was available because the library provides an IsSeq[String].
implicit val rangeRepr: IsIterable[Range] { type A = Int; type C = IndexedSeq[Int] } =
new IsIterable[Range] {
type A = Int
type C = IndexedSeq[Int]
def apply(coll: Range): IterableOps[Int, IndexedSeq, IndexedSeq[Int]] = coll
}
Attributes
- Companion
- object
- Source
- IsIterable.scala
- Supertypes
- Known subtypes
Attributes
- Companion
- trait
- Source
- IsIterable.scala
- Supertypes
- Self type
-
IsIterable.type
Attributes
- Source
- IsIterable.scala
- Supertypes
- Known subtypes
-
object IsIterable
Attributes
- Companion
- trait
- Source
- IsIterableOnce.scala
- Supertypes
- Self type
-
IsIterableOnce.type
Type class witnessing that a collection representation type Repr has elements of type A and has a conversion to IterableOnce[A].
Type class witnessing that a collection representation type Repr has elements of type A and has a conversion to IterableOnce[A].
This type enables simple enrichment of IterableOnces with extension methods which can make full use of the mechanics of the Scala collections framework in their implementation.
Example usage,
extension [Repr, I <: IsIterableOnce[Repr]](coll: Repr)(using it: I) {
final def filterMap[B, That](f: it.A => Option[B])(using bf: BuildFrom[Repr, B, That]): That = {
val b = bf.newBuilder(coll)
for(e <- it(coll).iterator) f(e).foreach(b += _)
b.result()
}
}
List(1, 2, 3, 4, 5).filterMap(i => if(i % 2 == 0) Some(i) else None)
// == List(2, 4)
Attributes
- Companion
- object
- Source
- IsIterableOnce.scala
- Supertypes
- Known subtypes
Attributes
- Source
- IsIterableOnce.scala
- Supertypes
- Known subtypes
-
object IsIterableOnce
Type class witnessing that a collection type Repr has keys of type K, values of type V and has a conversion to MapOps[K, V, Iterable, C], for some types K, V and C.
Type class witnessing that a collection type Repr has keys of type K, values of type V and has a conversion to MapOps[K, V, Iterable, C], for some types K, V and C.
This type enables simple enrichment of Maps with extension methods.
Type parameters
- Repr
-
Collection type (e.g.
Map[Int, String])
Attributes
- See also
- Companion
- object
- Source
- IsMap.scala
- Supertypes
Type class witnessing that a collection representation type Repr has elements of type A and has a conversion to SeqOps[A, Iterable, C], for some types A and C.
Type class witnessing that a collection representation type Repr has elements of type A and has a conversion to SeqOps[A, Iterable, C], for some types A and C.
This type enables simple enrichment of Seqs with extension methods which can make full use of the mechanics of the Scala collections framework in their implementation.
Attributes
- See also
- Companion
- object
- Source
- IsSeq.scala
- Supertypes
Deprecated classlikes
This trait represents collection-like objects that can be reduced using a '+' operator. It defines variants of - and -- as convenience methods in terms of single-element removal -.
This trait represents collection-like objects that can be reduced using a '+' operator. It defines variants of - and -- as convenience methods in terms of single-element removal -.
Type parameters
- A
-
the type of the elements of the $coll.
- Repr
-
the type of the $coll itself
Attributes
- Deprecated
-
[Since version 2.13.0]Subtractable is deprecated. This is now implemented as part of SetOps, MapOps, etc. - Source
- Subtractable.scala
- Supertypes
- Self type
-
Deprecated types
Attributes
- Deprecated
-
[Since version 2.13.0]Use scala.collection.BuildFrom instead - Source
- package.scala
Attributes
- Deprecated
-
[Since version 2.13.0]Clearable was moved from collection.generic to collection.mutable - Source
- package.scala
Attributes
- Deprecated
-
[Since version 2.13.0]Growable was moved from collection.generic to collection.mutable - Source
- package.scala
Attributes
- Deprecated
-
[Since version 2.13.0]Use IsIterable instead - Source
- package.scala
Attributes
- Deprecated
-
[Since version 2.13.0]Use IsIterableOnce instead - Source
- package.scala
Attributes
- Deprecated
-
[Since version 2.13.0]Shrinkable was moved from collection.generic to collection.mutable - Source
- package.scala