Skip to content

Commit

Permalink
Add Gen.either
Browse files Browse the repository at this point in the history
And make the Arbitrary instance for Either delegate to it.
  • Loading branch information
ceedubs committed May 12, 2018
1 parent 0f46e16 commit 9cb7319
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/scala/org/scalacheck/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private[scalacheck] sealed trait ArbitraryLowPriority {

/** Arbitrary instance of the Either type */
implicit def arbEither[T, U](implicit at: Arbitrary[T], au: Arbitrary[U]): Arbitrary[Either[T, U]] =
Arbitrary(oneOf(arbitrary[T].map(Left(_)), arbitrary[U].map(Right(_))))
Arbitrary(Gen.either(at.arbitrary, au.arbitrary))

/** Arbitrary instance of the Future type */
implicit def arbFuture[T](implicit a: Arbitrary[T]): Arbitrary[Future[T]] =
Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/org/scalacheck/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ object Gen extends GenArities{
def some[T](g: Gen[T]): Gen[Option[T]] =
g.map(Some.apply)

/** Generates a `Left` of `T` or a `Right` of `U` with equal probability. */
def either[T, U](gt: Gen[T], gu: Gen[U]): Gen[Either[T, U]] =
oneOf(gt.map(Left(_)), gu.map(Right(_)))

/** Chooses one of the given generators with a weighted random distribution */
def frequency[T](gs: (Int, Gen[T])*): Gen[T] = {
val filtered = gs.iterator.filter(_._1 > 0).toVector
Expand Down

0 comments on commit 9cb7319

Please sign in to comment.