Skip to content

Commit

Permalink
Merge pull request #59 from DanielaSfregola/#58_lazy_evaluation
Browse files Browse the repository at this point in the history
#58 Using lazy evaluation
  • Loading branch information
DanielaSfregola authored Apr 16, 2018
2 parents 7d35219 + d83cc3f commit aa7fcbc
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.danielasfregola.randomdatagenerator.utils.{SeedDetector, ShapelessLik
import org.scalacheck._

import scala.reflect.runtime.universe._
import scala.util.{Success, Try}

object RandomDataGenerator extends RandomDataGenerator

Expand All @@ -14,9 +15,11 @@ trait RandomDataGenerator extends ShapelessLike {
def random[T: WeakTypeTag: Arbitrary]: T = random(1).head

def random[T: WeakTypeTag: Arbitrary](n: Int): Seq[T] = {
val gen = Gen.listOfN(n, implicitly[Arbitrary[T]].arbitrary)
val optSeqT = gen.apply(Gen.Parameters.default, seed)
optSeqT.getOrElse(explode)
val gen = Gen.infiniteStream(implicitly[Arbitrary[T]].arbitrary)
Try(gen.apply(Gen.Parameters.default, seed)) match {
case Success(Some(v)) => v.take(n)
case _ => explode[T]
}
}

private def explode[T: WeakTypeTag]() = {
Expand Down

0 comments on commit aa7fcbc

Please sign in to comment.