mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-13 19:37:30 +01:00
Have sampleSome do a maximum of 10 attempts
This commit is contained in:
parent
d8e214bbbf
commit
af6c880a24
1 changed files with 15 additions and 4 deletions
|
@ -13,11 +13,22 @@ object Implicits {
|
|||
/** Extension methods for Scalacheck generatos */
|
||||
implicit class GeneratorOps[T](private val gen: Gen[T]) extends AnyVal {
|
||||
|
||||
@tailrec
|
||||
/** Gets a sample from this generator that's not `None` */
|
||||
def sampleSome: T = gen.sample match {
|
||||
case None => sampleSome
|
||||
case Some(sample) => sample
|
||||
def sampleSome: T = {
|
||||
val max = 10
|
||||
@tailrec
|
||||
def loop(counter: Int): T =
|
||||
if (counter > max) {
|
||||
sys.error(
|
||||
s"Could not get a sample from generator after $max attempts")
|
||||
} else {
|
||||
gen.sample match {
|
||||
case None => loop(counter + 1)
|
||||
case Some(sample) => sample
|
||||
}
|
||||
}
|
||||
|
||||
loop(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue