Skip to content

Add an iterator() alias to iterator #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/main/scala-2.11_2.12/collection/compat/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,37 @@ 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 StreamExtensionMethods[A](private val stream: Stream[A]) extends AnyVal {
def lazyAppendAll(as: => TraversableOnce[A]): Stream[A] = stream.append(as)
}

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]()
}
}
8 changes: 8 additions & 0 deletions src/test/scala/collection/FactoryTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ class FactoryTest {
Assert.assertEquals(1, counter) // One element has been evaluated because Stream is not lazy in its head
}

@Test
def streamFromPreservesLaziness(): Unit = {
var counter = 0
val source = Stream.continually { counter += 1; 1 }
val result = Stream.from(source)
Assert.assertEquals(1, counter) // One element has been evaluated because Stream is not lazy in its head
}

}
14 changes: 14 additions & 0 deletions src/test/scala/collection/IterableLikeTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package collection

import org.junit.Test

import scala.collection.compat._

class IterableLikeTest {

@Test
def iteratorTest(): Unit = {
List(1, 2, 3).iterator()
}

}