Skip to content

Commit

Permalink
Go back to using Traversable instead of Iterable
Browse files Browse the repository at this point in the history
This is a no-op for 2.13, as Traversable is a (deprecated) alias for
Iterable. For 2.10-2.12, this ensures that the signatures don't change
due to cross-building with 2.13.
  • Loading branch information
lrytz committed May 23, 2018
1 parent 5ba8ddf commit 1c79131
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/org/scalacheck/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,17 @@ private[scalacheck] sealed trait ArbitraryLowPriority {
Arbitrary(Gen.oneOf(arbitrary[T].map(Success(_)), arbitrary[Throwable].map(Failure(_))))

/** Arbitrary instance of any [[org.scalacheck.util.Buildable]] container
* (such as lists, arrays, lazy lists, etc). The maximum size of the container
* (such as lists, arrays, streams / lazy lists, etc). The maximum size of the container
* depends on the size generation parameter. */
implicit def arbContainer[C[_],T](implicit
a: Arbitrary[T], b: Buildable[T,C[T]], t: C[T] => Iterable[T]
a: Arbitrary[T], b: Buildable[T,C[T]], t: C[T] => Traversable[T]
): Arbitrary[C[T]] = Arbitrary(buildableOf[C[T],T](arbitrary[T]))

/** Arbitrary instance of any [[org.scalacheck.util.Buildable]] container
* (such as maps). The maximum size of the container depends on the size
* generation parameter. */
implicit def arbContainer2[C[_,_],T,U](implicit
a: Arbitrary[(T,U)], b: Buildable[(T,U),C[T,U]], t: C[T,U] => Iterable[(T,U)]
a: Arbitrary[(T,U)], b: Buildable[(T,U),C[T,U]], t: C[T,U] => Traversable[(T,U)]
): Arbitrary[C[T,U]] = Arbitrary(buildableOf[C[T,U],(T,U)](arbitrary[(T,U)]))

implicit def arbEnum[A <: java.lang.Enum[A]](implicit A: reflect.ClassTag[A]): Arbitrary[A] = {
Expand Down
22 changes: 11 additions & 11 deletions src/main/scala/org/scalacheck/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -577,55 +577,55 @@ object Gen extends GenArities with GenVersionSpecific {

//// List Generators ////

/** Generates a container of any Iterable type for which there exists an
/** Generates a container of any Traversable type for which there exists an
* implicit [[org.scalacheck.util.Buildable]] instance. The elements in the
* container will be generated by the given generator. The size of the
* generated container is limited by `n`. Depending on what kind of container
* that is generated, the resulting container may contain fewer elements than
* `n`, but not more. If the given generator fails generating a value, the
* complete container generator will also fail. */
def buildableOfN[C,T](n: Int, g: Gen[T])(implicit
evb: Buildable[T,C], evt: C => Iterable[T]
evb: Buildable[T,C], evt: C => Traversable[T]
): Gen[C] =
sequence[C,T](Iterable.fill(n)(g)) suchThat { c =>
sequence[C,T](Traversable.fill(n)(g)) suchThat { c =>
// TODO: Can we guarantee c.size == n (See issue #89)?
c.forall(g.sieveCopy)
}

/** Generates a container of any Iterable type for which there exists an
/** Generates a container of any Traversable type for which there exists an
* implicit [[org.scalacheck.util.Buildable]] instance. The elements in the
* container will be generated by the given generator. The size of the
* container is bounded by the size parameter used when generating values. */
def buildableOf[C,T](g: Gen[T])(implicit
evb: Buildable[T,C], evt: C => Iterable[T]
evb: Buildable[T,C], evt: C => Traversable[T]
): Gen[C] =
sized(s => choose(0, s max 0).flatMap(buildableOfN[C,T](_,g))) suchThat { c =>
if (c == null) g.sieveCopy(null) else c.forall(g.sieveCopy)
}

/** Generates a non-empty container of any Iterable type for which there
/** Generates a non-empty container of any Traversable type for which there
* exists an implicit [[org.scalacheck.util.Buildable]] instance. The
* elements in the container will be generated by the given generator. The
* size of the container is bounded by the size parameter used when
* generating values. */
def nonEmptyBuildableOf[C,T](g: Gen[T])(implicit
evb: Buildable[T,C], evt: C => Iterable[T]
evb: Buildable[T,C], evt: C => Traversable[T]
): Gen[C] =
sized(s => choose(1, s max 1).flatMap(buildableOfN[C,T](_,g))) suchThat(_.size > 0)

/** A convenience method for calling `buildableOfN[C[T],T](n,g)`. */
def containerOfN[C[_],T](n: Int, g: Gen[T])(implicit
evb: Buildable[T,C[T]], evt: C[T] => Iterable[T]
evb: Buildable[T,C[T]], evt: C[T] => Traversable[T]
): Gen[C[T]] = buildableOfN[C[T],T](n,g)

/** A convenience method for calling `buildableOf[C[T],T](g)`. */
def containerOf[C[_],T](g: Gen[T])(implicit
evb: Buildable[T,C[T]], evt: C[T] => Iterable[T]
evb: Buildable[T,C[T]], evt: C[T] => Traversable[T]
): Gen[C[T]] = buildableOf[C[T],T](g)

/** A convenience method for calling `nonEmptyBuildableOf[C[T],T](g)`. */
def nonEmptyContainerOf[C[_],T](g: Gen[T])(implicit
evb: Buildable[T,C[T]], evt: C[T] => Iterable[T]
evb: Buildable[T,C[T]], evt: C[T] => Traversable[T]
): Gen[C[T]] = nonEmptyBuildableOf[C[T],T](g)

/** Generates a list of random length. The maximum length depends on the
Expand Down Expand Up @@ -656,7 +656,7 @@ object Gen extends GenArities with GenVersionSpecific {
* is equal to calling <code>containerOfN[Map,T,U](n,g)</code>. */
def mapOfN[T,U](n: Int, g: Gen[(T,U)]) = buildableOfN[Map[T,U],(T,U)](n,g)

/** Generates an infinite lazy list. */
/** Generates an infinite stream. */
def infiniteStream[T](g: => Gen[T]): Gen[Stream[T]] = {
def unfold[A, S](z: S)(f: S => Option[(A, S)]): Stream[A] = f(z) match {
case Some((h, s)) => h #:: unfold(s)(f)
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/org/scalacheck/Shrink.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Shrink extends ShrinkLowPriority {
import LazyList.{cons, empty}
import scala.collection._

/** Interleaves two lazy lists */
/** Interleaves two streams / lazy lists */
private def interleave[T](xs: LazyList[T], ys: LazyList[T]): LazyList[T] =
if(xs.isEmpty) ys
else if(ys.isEmpty) xs
Expand All @@ -45,12 +45,12 @@ object Shrink extends ShrinkLowPriority {
def shrink[T](x: T)(implicit s: Shrink[T]): LazyList[T] = s.shrink(x)

/** Shrink a value, but also return the original value as the first element in
* the resulting lazy list */
* the resulting stream / lazy list */
def shrinkWithOrig[T](x: T)(implicit s: Shrink[T]): LazyList[T] =
cons(x, s.shrink(x))

/** Shrink instance of container */
implicit def shrinkContainer[C[_],T](implicit v: C[T] => Iterable[T], s: Shrink[T],
implicit def shrinkContainer[C[_],T](implicit v: C[T] => Traversable[T], s: Shrink[T],
b: Buildable[T,C[T]]
): Shrink[C[T]] = Shrink { xs: C[T] =>
val ys = v(xs)
Expand All @@ -59,7 +59,7 @@ object Shrink extends ShrinkLowPriority {
}

/** Shrink instance of container2 */
implicit def shrinkContainer2[C[_,_],T,U](implicit v: C[T,U] => Iterable[(T,U)], s: Shrink[(T,U)],
implicit def shrinkContainer2[C[_,_],T,U](implicit v: C[T,U] => Traversable[(T,U)], s: Shrink[(T,U)],
b: Buildable[(T,U),C[T,U]]
): Shrink[C[T,U]] = Shrink { xs: C[T,U] =>
val ys = v(xs)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/org/scalacheck/util/Buildable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ trait Buildable[T,C] extends Serializable {
* Factory instances implementing Serializable, so that the objects capturing those can be
* serializable too.
*/
// Names are `..CanBuildFrom` for binary compatibility. Change to `..Factory` in a major release.
object SerializableCanBuildFroms {
// Names are `..CanBuildFrom` for binary compatibility. Change to `..Factory` in a major release.
implicit def listCanBuildFrom[T]: Factory[T, List[T]] = ScalaVersionSpecific.listFactory
implicit def bitsetCanBuildFrom[T]: Factory[Int, BitSet] = ScalaVersionSpecific.bitsetFactory
implicit def mapCanBuildFrom[T, U]: Factory[(T, U), Map[T, U]] = ScalaVersionSpecific.mapFactory
Expand All @@ -41,7 +41,7 @@ object Buildable {
}

import java.util.ArrayList
implicit def buildableArrayList[T] = new Buildable[T,ArrayList[T]] {
implicit def buildableArrayList[T]: Buildable[T, ArrayList[T]] = new Buildable[T,ArrayList[T]] {
def builder = new ArrayListBuilder[T]
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/org/scalacheck/ShrinkSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ object ShrinkSpecification extends Properties("Shrink") {

/* Ensure that shrink[T] terminates. (#244)
*
* Let's say shrinking "terminates" when the lazy list of values
* Let's say shrinking "terminates" when the stream / lazy list of values
* becomes empty. We can empirically determine the longest possible
* sequence for a given type before termination. (Usually this
* involves using the type's MinValue.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ScalaVersionSpecific._
object BuildableSpecification {
def container[C[_]](implicit
evb: Buildable[String, C[String]],
evt: C[String] => Iterable[String]
evt: C[String] => Traversable[String]
) = Gen.containerOf[C, String](Gen.alphaStr)

implicit val listGen: Gen[List[String]] = container[List]
Expand Down

0 comments on commit 1c79131

Please sign in to comment.