Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Representable instance for Eval #2329

Merged
merged 2 commits into from
Jul 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -416,6 +416,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