Skip to content

Commit

Permalink
Binested sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
iravid committed May 18, 2018
1 parent 002bfc2 commit 05d5329
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions core/src/main/scala/cats/data/Binested.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cats
package data

import cats.arrow.Profunctor

final case class Binested[F[_, _], G[_], H[_], A, B](value: F[G[A], H[B]])

object Binested {
implicit def catsDataEqForBinested[F[_, _], G[_], H[_], A, B](implicit F: Eq[F[G[A], H[B]]]): Eq[Binested[F, G, H, A, B]] =
Eq.by(_.value)

implicit def catsDataBifunctorForBinested[F[_, _], G[_], H[_]](
implicit F: Bifunctor[F], G: Functor[G], H: Functor[H]) =
new Bifunctor[λ[(ɑ, β) => Binested[F, G, H, ɑ, β]]] {
def bimap[A, B, C, D](fab: Binested[F, G, H, A, B])(f: A => C, g: B => D): Binested[F, G, H, C, D] =
Binested(F.bimap(fab.value)(G.map(_)(f), H.map(_)(g)))
}

implicit def catsDataProfunctorForBinested[F[_, _], G[_], H[_]](
implicit F: Profunctor[F], G: Functor[G], H: Functor[H]) =
new Profunctor[λ[(ɑ, β) => Binested[F, G, H, ɑ, β]]] {
def dimap[A, B, C, D](fab: Binested[F, G, H, A, B])(f: C => A)(g: B => D): Binested[F, G, H, C, D] =
Binested(F.dimap(fab.value)(G.map(_: G[C])(f))(H.map(_)(g)))
}
}
3 changes: 3 additions & 0 deletions laws/src/main/scala/cats/laws/discipline/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ object arbitrary extends ArbitraryInstances0 {
implicit def catsLawsArbitraryForNested[F[_], G[_], A](implicit FG: Arbitrary[F[G[A]]]): Arbitrary[Nested[F, G, A]] =
Arbitrary(FG.arbitrary.map(Nested(_)))

implicit def catsLawsArbitraryForBinested[F[_, _], G[_], H[_], A, B](implicit F: Arbitrary[F[G[A], H[B]]]): Arbitrary[Binested[F, G, H, A, B]] =
Arbitrary(F.arbitrary.map(Binested(_)))

implicit def catsLawArbitraryForState[S: Arbitrary: Cogen, A: Arbitrary]: Arbitrary[State[S, A]] =
catsLawArbitraryForIndexedStateT[Eval, S, S, A]

Expand Down
31 changes: 31 additions & 0 deletions tests/src/test/scala/cats/tests/BinestedSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cats
package tests

import cats.arrow.Profunctor
import cats.data.Binested

import cats.laws.discipline._
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.eq._

class BinestedSuite extends CatsSuite {
// we have a lot of generated lists of lists in these tests. We have to tell
// Scalacheck to calm down a bit so we don't hit memory and test duration
// issues.
implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
PropertyCheckConfiguration(minSuccessful = 20, sizeRange = 5)

{
// Bifunctor + Functor + Functor = Bifunctor
implicit val instance = ListWrapper.functor
checkAll("Binested[Either, ListWrapper, Option, ?, ?]", BifunctorTests[Binested[Either, ListWrapper, Option, ?, ?]].bifunctor[Int, Int, Int, String, String, String])
checkAll("Bifunctor[Binested[Either, ListWrapper, Option, ?, ?]]", SerializableTests.serializable(Bifunctor[Binested[Either, ListWrapper, Option, ?, ?]]))
}

{
// Profunctor + Functor + Functor = Profunctor
implicit val instance = ListWrapper.functor
checkAll("Binested[Function1, ListWrapper, Option, ?, ?]", ProfunctorTests[Binested[Function1, ListWrapper, Option, ?, ?]].profunctor[Int, Int, Int, String, String, String])
checkAll("Profunctor[Binested[Function1, ListWrapper, Option, ?, ?]]", SerializableTests.serializable(Profunctor[Binested[Function1, ListWrapper, Option, ?, ?]]))
}
}

0 comments on commit 05d5329

Please sign in to comment.