Skip to content

Commit 405c3ec

Browse files
bakeemawaytoysjbgi
authored andcommitted
Added Scalacheck Arbitrary implementations for Natural and NonEmptyList.
1 parent 07f94fa commit 405c3ec

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fj.data
2+
3+
import org.scalacheck.Arbitrary
4+
5+
/**
6+
* A Scalacheck [[Arbitrary]] for [[Natural]].
7+
*/
8+
object ArbitraryNatural {
9+
10+
implicit def arbitraryNatural: Arbitrary[Natural] =
11+
Arbitrary(Arbitrary.arbBigInt.arbitrary.map(_.abs).map(bi => Natural.natural(bi.bigInteger).some()))
12+
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package fj.data
2+
3+
import fj.data.NonEmptyList.nel
4+
import org.scalacheck.Arbitrary.arbitrary
5+
import org.scalacheck.{Arbitrary, Gen}
6+
7+
/**
8+
* A Scalacheck [[Arbitrary]] for [[NonEmptyList]].
9+
*/
10+
object ArbitraryNonEmptyList {
11+
implicit def arbitraryNonEmptyList[A](implicit a: Arbitrary[A]): Arbitrary[NonEmptyList[A]] =
12+
Arbitrary(nelOf(arbitrary[A]))
13+
14+
def nelOf[A](g: => Gen[A]): Gen[NonEmptyList[A]] =
15+
Gen.nonEmptyListOf(g).map(l => l.tail.foldRight(nel(l.head))((x, n) => n.cons(x)))
16+
}

0 commit comments

Comments
 (0)