-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cats | ||
package data | ||
|
||
import cats.arrow._ | ||
|
||
/** Compose a two-slot type constructor `F[_, _]` with two single-slot type constructors | ||
* `G[_]` and `H[_]`, resulting in a two-slot type constructor with respect to the inner types. | ||
* For example, `List` and `Option` both have `Functor` instances, and `Either` has a | ||
* `Bifunctor` instance. Therefore, `Binested[Either, List, Option, ?, ?]` has a `Bifunctor` | ||
* instance as well: | ||
* | ||
* {{{ | ||
* scala> import cats.Bifunctor | ||
* scala> import cats.data.Binested | ||
* scala> import cats.implicits._ | ||
* scala> val eitherListOption: Either[List[Int], Option[String]] = Right(Some("cats")) | ||
* scala> val f: Int => String = _.toString | ||
* scala> val g: String => String = _ + "-bifunctor" | ||
* scala> val binested = Binested(eitherListOption) | ||
* scala> val bimapped = Bifunctor[Binested[Either, List, Option, ?, ?]].bimap(binested)(f, g).value | ||
* res0: Either[List[String], Option[String]] = Right(Some("cats-bifunctor")) | ||
* }}} | ||
*/ | ||
final case class Binested[F[_, _], G[_], H[_], A, B](value: F[G[A], H[B]]) | ||
|
||
object Binested extends BinestedInstances | ||
|
||
trait BinestedInstances extends BinestedInstances0 { | ||
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 catsDataProfunctorForBinested[F[_, _], G[_], H[_]]( | ||
implicit F: Profunctor[F], G: Functor[G], H: Functor[H]): Profunctor[Binested[F, G, 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))) | ||
} | ||
|
||
implicit def catsDataBitraverseForBinested[F[_, _], G[_], H[_]]( | ||
implicit F0: Bitraverse[F], H0: Traverse[H], G0: Traverse[G]): Bitraverse[Binested[F, G, H, ?, ?]] = | ||
new BinestedBitraverse[F, G, H] { | ||
override implicit def F: Bitraverse[F] = F0 | ||
override implicit def G: Traverse[G] = G0 | ||
override implicit def H: Traverse[H] = H0 | ||
} | ||
} | ||
|
||
trait BinestedInstances0 { | ||
implicit def catsDataBifoldableForBinested[F[_, _], G[_], H[_]]( | ||
implicit F0: Bifoldable[F], G0: Foldable[G], H0: Foldable[H]): Bifoldable[Binested[F, G, H, ?, ?]] = | ||
new BinestedBifoldable[F, G, H] { | ||
override implicit def F: Bifoldable[F] = F0 | ||
override implicit def G: Foldable[G] = G0 | ||
override implicit def H: Foldable[H] = H0 | ||
} | ||
|
||
implicit def catsDataBifunctorForBinested[F[_, _], G[_], H[_]]( | ||
implicit F: Bifunctor[F], G: Functor[G], H: Functor[H]): Bifunctor[Binested[F, G, 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))) | ||
} | ||
} | ||
|
||
sealed abstract class BinestedBifoldable[F[_, _], G[_], H[_]] extends Bifoldable[Binested[F, G, H, ?, ?]] { | ||
implicit def F: Bifoldable[F] | ||
implicit def G: Foldable[G] | ||
implicit def H: Foldable[H] | ||
|
||
def bifoldLeft[A, B, C](fab: Binested[F, G, H, A, B], c: C)(f: (C, A) => C, g: (C, B) => C): C = | ||
F.bifoldLeft(fab.value, c)( | ||
(c, ga) => G.foldLeft(ga, c)(f), | ||
(c, hb) => H.foldLeft(hb, c)(g) | ||
) | ||
|
||
|
||
def bifoldRight[A, B, C](fab: Binested[F, G, H, A, B], c: Eval[C])(f: (A, Eval[C]) => Eval[C], g: (B, Eval[C]) => Eval[C]): Eval[C] = | ||
F.bifoldRight(fab.value, c)( | ||
(ga, ec) => G.foldRight(ga, ec)(f), | ||
(hb, ec) => H.foldRight(hb, ec)(g) | ||
) | ||
} | ||
|
||
sealed abstract class BinestedBitraverse[F[_, _], G[_], H[_]] extends BinestedBifoldable[F, G, H] with Bitraverse[Binested[F, G, H, ?, ?]] { | ||
override implicit def F: Bitraverse[F] | ||
override implicit def G: Traverse[G] | ||
override implicit def H: Traverse[H] | ||
|
||
def bitraverse[I[_], A, B, C, D](fab: Binested[F, G, H, A, B])(f: A => I[C], g: B => I[D])(implicit I: Applicative[I]): I[Binested[F, G, H, C, D]] = { | ||
I.map(F.bitraverse(fab.value)(G.traverse(_)(f), H.traverse(_)(g)))(Binested(_)) | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cats | ||
package syntax | ||
|
||
import cats.data.Binested | ||
|
||
trait BinestedSyntax { | ||
implicit final def catsSyntaxBinestedId[F[_, _], G[_], H[_], A, B](value: F[G[A], H[B]]): BinestedIdOps[F, G, H, A, B] = | ||
new BinestedIdOps(value) | ||
} | ||
|
||
final class BinestedIdOps[F[_, _], G[_], H[_], A, B](val value: F[G[A], H[B]]) extends AnyVal { | ||
def binested: Binested[F, G, H, A, B] = Binested(value) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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, ?, ?]])) | ||
} | ||
|
||
{ | ||
// Bifoldable + foldable + foldable = Bifoldable | ||
implicit val instance = ListWrapper.foldable | ||
checkAll("Binested[Either, ListWrapper, ListWrapper, ?, ?]", BifoldableTests[Binested[Either, ListWrapper, ListWrapper, ?, ?]].bifoldable[Int, Int, Int]) | ||
checkAll("Bifoldable[Binested[Either, ListWrapper, ListWrapper, ?, ?]]", SerializableTests.serializable(Bifoldable[Binested[Either, ListWrapper, ListWrapper, ?, ?]])) | ||
} | ||
|
||
{ | ||
// Bitraverse + traverse + traverse = Bitraverse | ||
implicit val instance = ListWrapper.traverse | ||
checkAll("Binested[Either, ListWrapper, ListWrapper, ?, ?]", BitraverseTests[Binested[Either, ListWrapper, ListWrapper, ?, ?]].bitraverse[Option, Int, Int, Int, String, String, String]) | ||
checkAll("Bitraverse[Binested[Either, ListWrapper, ListWrapper, ?, ?]]", SerializableTests.serializable(Bitraverse[Binested[Either, ListWrapper, ListWrapper, ?, ?]])) | ||
} | ||
|
||
test("simple syntax-based usage") { | ||
forAll { value: (Option[Int], List[Int]) => | ||
value.binested.bimap(_.toString, _.toString).value should ===(value.bimap(_.map(_.toString), _.map(_.toString))) | ||
} | ||
} | ||
} |
This file contains 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