Skip to content

Commit

Permalink
typelevel#2947 Added bifold to Bifoldable typeclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Twizty committed Sep 27, 2019
1 parent 41d2f76 commit b9c5b3e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/Bifoldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import simulacrum.typeclass
val F = self
val G = ev
}

def bifold[A, B](fab: F[A, B])(implicit A: Monoid[A], B: Monoid[B], C: Monoid[(A, B)]): (A, B) =
bifoldMap(fab)((_, B.empty), (A.empty, _))
}

private[cats] trait ComposedBifoldable[F[_, _], G[_, _]] extends Bifoldable[λ[(α, β) => F[G[α, β], G[α, β]]]] {
Expand Down
5 changes: 5 additions & 0 deletions tests/src/test/scala/cats/tests/BifoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ class BifoldableSuite extends CatsSuite {

checkAll("Either compose Either", BifoldableTests(eitherComposeEither).bifoldable[Int, Int, Int])
checkAll("Bifoldable[Either compose Either]", SerializableTests.serializable(eitherComposeEither))

test("bifold works for 2 monoids") {
Bifoldable[Either].bifold(Either.right[Int, String]("something")) should === ((0, "something"))
Bifoldable[Either].bifold(Either.left[Int, String](5)) should === ((5, ""))
}
}

0 comments on commit b9c5b3e

Please sign in to comment.