Created
December 25, 2018 10:36
-
-
Save yasuabe/4e02b7fbfcb0fc688e27b09488c6f67a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package representable01 | |
import cats.instances.AllInstances | |
import cats.laws.discipline.RepresentableTests | |
import cats.{Apply, Functor, Representable} | |
import org.scalacheck.{Arbitrary, Gen} | |
import org.specs2.Specification | |
import org.typelevel.discipline.specs2.Discipline | |
import cats.syntax.apply._ | |
object Triple { | |
type Triple[A] = (A, A, A) | |
implicit val functorTriple: Functor[Triple] = new Functor[Triple] { | |
def map[A, B](fa: Triple[A])(f: A => B): Triple[B] = (f(fa._1), f(fa._2), f(fa._3)) | |
} | |
implicit val repTriple0: Representable.Aux[Triple, Int] = new Representable[Triple] { | |
type Representation = Int | |
def F = Functor[Triple] | |
def index[A](f: Triple[A]): Int => A = n => | |
if (n < 0) f._1 | |
else if (n > 0) f._3 | |
else f._2 | |
def tabulate[A](f: Int => A): Triple[A] = F.map(-1, 0, 1)(f) | |
} | |
} | |
import Triple._ | |
class TripleSpec extends Specification with Discipline with AllInstances { | |
implicit val applyGen: Apply[Gen] = new Apply[Gen] { | |
def ap[A, B](ff: Gen[A => B])(fa: Gen[A]): Gen[B] = ff flatMap (f => fa.map(f)) | |
def map[A, B](fa: Gen[A])(f: A => B): Gen[B] = fa map f | |
} | |
implicit val arbIntTriple: Arbitrary[Triple[Int]] = Arbitrary { | |
val smallInt = Gen.chooseNum(-2, 2) | |
(smallInt, smallInt, smallInt) mapN { (_, _, _) } | |
} | |
implicit val arbFunc: Arbitrary[Int => Char] = Arbitrary { | |
Gen.oneOf((' ', ' ', ' '), ('a', 'b', 'c'), ('-', '0', '+')) map { case (x, y, z) => | |
(n: Int) => if (n < 0) x else if (n > 0) y else z | |
} | |
} | |
private val check = checkAll("Representable of Triple", RepresentableTests[Triple, Int].representable[Char]) | |
def is = s2"Representable of Triple satisfies representable laws $check" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment