Skip to content

Commit bb4ff52

Browse files
committed
Add scala.collection.Factory
1 parent fcc3425 commit bb4ff52

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package scala.collection
2+
3+
import scala.collection.generic.CanBuildFrom
4+
5+
/**
6+
* A factory that builds a collection of type `C` with elements of type `A`.
7+
*
8+
* @tparam A Type of elements (e.g. `Int`, `Boolean`, etc.)
9+
* @tparam C Type of collection (e.g. `List[Int]`, `TreeMap[Int, String]`, etc.)
10+
*/
11+
trait Factory[-A, +C] extends Any {
12+
13+
/**
14+
* @return A collection of type `C` containing the same elements
15+
* as the source collection `it`.
16+
* @param it Source collection
17+
*/
18+
def fromSpecific(it: TraversableOnce[A]): C
19+
20+
/** Get a Builder for the collection. For non-strict collection types this will use an intermediate buffer.
21+
* Building collections with `fromSpecific` is preferred because it can be lazy for lazy collections. */
22+
def newBuilder(): mutable.Builder[A, C]
23+
}
24+
25+
object Factory {
26+
27+
implicit def fromCanBuildFrom[A, C](implicit cbf: CanBuildFrom[Nothing, A, C]): Factory[A, C] =
28+
new Factory[A, C] {
29+
def fromSpecific(it: TraversableOnce[A]): C = (cbf() ++= it).result()
30+
def newBuilder(): mutable.Builder[A, C] = cbf()
31+
}
32+
33+
implicit def fromCanBuildFromConversion[X, A, C](x: X)(implicit toCanBuildFrom: X => CanBuildFrom[Nothing, A, C]): Factory[A, C] =
34+
fromCanBuildFrom(toCanBuildFrom(x))
35+
36+
}

src/main/scala-2.12/collection/compat/package.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ package object compat {
2323
implicit def sortedMapFactoryToCBF[K : Ordering, V, CC[A, B] <: SortedMap[A, B] with SortedMapLike[A, B, CC[A, B]]](fact: SortedMapFactory[CC]): CanBuildFrom[Any, (K, V), CC[K, V]] =
2424
simpleCBF(fact.newBuilder[K, V])
2525

26+
implicit def bitSetFactoryToCBF(fact: BitSetFactory[BitSet]): CanBuildFrom[Any, Int, BitSet] =
27+
simpleCBF(fact.newBuilder)
28+
2629
implicit def immutableBitSetFactoryToCBF(fact: BitSetFactory[immutable.BitSet]): CanBuildFrom[Any, Int, ImmutableBitSetCC[Int]] =
2730
simpleCBF(fact.newBuilder)
2831

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package collection
2+
3+
import scala.collection.{Factory, Iterable, BitSet, mutable, immutable}
4+
import scala.collection.compat._
5+
6+
class FactoryTest {
7+
8+
implicitly[Factory[Char, String]]
9+
implicitly[Factory[Char, Array[Char]]]
10+
implicitly[Factory[Int, BitSet]]
11+
implicitly[Factory[Int, mutable.BitSet]]
12+
implicitly[Factory[Int, immutable.BitSet]]
13+
14+
BitSet: Factory[Int, BitSet]
15+
Iterable: Factory[Int, Iterable[Int]]
16+
immutable.TreeSet: Factory[Int, immutable.TreeSet[Int]]
17+
Map: Factory[(Int, String), Map[Int, String]]
18+
immutable.TreeMap: Factory[(Int, String), immutable.TreeMap[Int, String]]
19+
20+
}

0 commit comments

Comments
 (0)