implicit def ArbitraryList: Arbitrary[Int] =
Arbitrary(distinct((1 to 5).toList))
def distinct[T](list: List[T]): Gen[T] = {
var seen = List[T]()
Gen.delay {
if (seen.size == list.size)
seen = List()
Gen.oneOf(list.filterNot(seen.contains)).sample match {
case Some(x) =>
seen = x +: seen
Gen.const(x)
case None =>
Gen.oneOf(list)
}
}
}
If I print out the generated elements I get:
1
2
3
4
5
2
1
4
5
3
5
1
4
2
3
4
1
2
3
5
3
5
1
2
4
Eric.