Skip to content

Commit

Permalink
Add (partial) support for newBuilder() (with parens)
Browse files Browse the repository at this point in the history
Also add more support  for `from` to more companion types, and
make their implementation more efficient.
  • Loading branch information
julienrf committed Mar 29, 2018
1 parent 88da007 commit 64653f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/main/scala-2.12/collection/compat/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,33 @@ package object compat {
simpleCBF(fact.newBuilder)

implicit class IterableFactoryExtensionMethods[CC[X] <: GenTraversable[X]](private val fact: GenericCompanion[CC]) {
def from[A](source: TraversableOnce[A]): CC[A] = fact.apply(source.toSeq: _*)
def from[A](source: TraversableOnce[A]): CC[A] = (fact.newBuilder[A] ++= source).result()
}

implicit class MapFactoryExtensionMethods[CC[A, B] <: Map[A, B] with MapLike[A, B, CC[A, B]]](private val fact: MapFactory[CC]) {
def from[K, V](source: TraversableOnce[(K, V)]): CC[K, V] = fact.apply(source.toSeq: _*)
def from[K, V](source: TraversableOnce[(K, V)]): CC[K, V] = (fact.newBuilder[K, V] ++= source).result()
}

implicit class SortedMapFactoryExtensionMethods[CC[A, B] <: SortedMap[A, B] with SortedMapLike[A, B, CC[A, B]]](private val fact: SortedMapFactory[CC]) extends AnyVal {
def from[K : Ordering, V](source: TraversableOnce[(K, V)]): CC[K, V] = (fact.newBuilder[K, V] ++= source).result()
}

implicit class BitSetFactoryExtensionMethods[C <: scala.collection.BitSet with scala.collection.BitSetLike[C]](private val fact: BitSetFactory[C]) {
def fromSpecific(source: TraversableOnce[Int]): C = fact.apply(source.toSeq: _*)
def fromSpecific(source: TraversableOnce[Int]): C = (fact.newBuilder ++= source).result()
}

implicit class SortedSetFactoryExtensionMethods[CC[X] <: SortedSet[X] with SortedSetLike[X, CC[X]]](private val fact: SortedSetFactory[CC]) extends AnyVal {
def from[A : Ordering](source: TraversableOnce[A]): CC[A] = (fact.newBuilder[A] ++= source).result()
}

implicit class WithParens[A](private val as: IterableLike[A, _]) extends AnyVal {
implicit class IterableExtensionMethods[A](private val as: IterableLike[A, _]) extends AnyVal {
def iterator(): Iterator[A] = as.iterator
}

implicit class BuilderExtensionMethods[Elem, To](val builder: Builder[Elem, To]) extends AnyVal {
def apply(): builder.type = builder
}

// This really belongs into scala.collection but there's already a package object in scala-library so we can't add to it
type IterableOnce[+X] = TraversableOnce[X]
}
22 changes: 21 additions & 1 deletion src/test/scala/collection/CollectionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util
import org.junit.Test
import org.junit.Assert._

import scala.collection.immutable.BitSet
import scala.collection.immutable.{BitSet, TreeMap, TreeSet}
import scala.collection.compat._

class CollectionTest {
Expand Down Expand Up @@ -47,5 +47,25 @@ class CollectionTest {
val m = Map.from(ys)
val mT: Map[Int, String] = m
assertEquals(Map(1 -> "a", 2 -> "b"), m)

val ts = TreeSet.from(xs)
val tsT: TreeSet[Int] = ts
assertEquals(TreeSet(1, 2, 3), ts)

val tm = TreeMap.from(ys)
val tmT: TreeMap[Int, String] = tm
assertEquals(TreeMap(1 -> "a", 2 -> "b"), tm)
}

@Test
def testNewBuilder(): Unit = {
List.newBuilder[Int]()
Vector.newBuilder[String]()
Map.newBuilder[Int, String]()
BitSet.newBuilder()
// The following cases don’t work because the `newBuilder` method takes implicit parameters
// Array.newBuilder[Int]()
// TreeSet.newBuilder[Int]()
// TreeMap.newBuilder[Int, String]()
}
}

0 comments on commit 64653f5

Please sign in to comment.