Skip to content

Commit

Permalink
Add Representable instance for Eval (#2329)
Browse files Browse the repository at this point in the history
* Add Representable instance for Eval

* Change F from def to val
  • Loading branch information
denisrosca authored and kailuowang committed Jul 22, 2018
1 parent 6f049ad commit 4730884
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions core/src/main/scala/cats/Eval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,22 @@ private[cats] sealed abstract class EvalInstances extends EvalInstances0 {

implicit def catsGroupForEval[A: Group]: Group[Eval[A]] =
new EvalGroup[A] { val algebra: Group[A] = Group[A] }

implicit val catsRepresentableForEval: Representable.Aux[Eval, Unit] = new Representable[Eval] {
override type Representation = Unit

override val F: Functor[Eval] = Functor[Eval]

/**
* Create a function that "indexes" into the `F` structure using `Representation`
*/
override def index[A](f: Eval[A]): Unit => A = (_: Unit) => f.value

/**
* Reconstructs the `F` structure using the index function
*/
override def tabulate[A](f: Unit => A): Eval[A] = Eval.later(f(()))
}
}

private[cats] sealed abstract class EvalInstances0 extends EvalInstances1 {
Expand Down
4 changes: 3 additions & 1 deletion tests/src/test/scala/cats/tests/RepresentableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.laws.discipline.SemigroupalTests.Isomorphisms
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.eq._
import cats.laws.discipline.{BimonadTests, MonadTests, RepresentableTests, SerializableTests}
import cats.{Bimonad, Eq, Id, Representable}
import cats.{Bimonad, Eq, Eval, Id, Representable}
import org.scalacheck.Arbitrary
import cats.data.Kleisli

Expand All @@ -21,6 +21,8 @@ class RepresentableSuite extends CatsSuite {
checkAll("Pair[String, String] <-> Boolean => String", RepresentableTests[Pair, Boolean].representable[String])
checkAll("Representable[Pair]", SerializableTests.serializable(Representable[Pair]))

checkAll("Eval[Int] <-> Unit => Int", RepresentableTests[Eval, Unit].representable[Int])
checkAll("Representable[Eval]", SerializableTests.serializable(Representable[Eval]))

{
implicit val representableKleisliPair = Kleisli.catsDataRepresentableForKleisli[Pair, Boolean, String]
Expand Down

0 comments on commit 4730884

Please sign in to comment.