From 5ad3c4026422f3fd3aa24cded49d2e48423688ad Mon Sep 17 00:00:00 2001 From: Maxim Davydov Date: Tue, 5 Nov 2019 18:23:22 +0300 Subject: [PATCH] #2947 Added bifold to Bifoldable typeclass (#3088) * #2947 Added bifold to Bifoldable typeclass * #2947 Formatted * address Luka's feedback * reformat --- core/src/main/scala/cats/Bifoldable.scala | 5 +++++ tests/src/test/scala/cats/tests/BifoldableSuite.scala | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/core/src/main/scala/cats/Bifoldable.scala b/core/src/main/scala/cats/Bifoldable.scala index ae49e72d32..4098cbbf27 100644 --- a/core/src/main/scala/cats/Bifoldable.scala +++ b/core/src/main/scala/cats/Bifoldable.scala @@ -25,6 +25,11 @@ import simulacrum.typeclass val F = self val G = ev } + + def bifold[A, B](fab: F[A, B])(implicit A: Monoid[A], B: Monoid[B]): (A, B) = { + import cats.instances.tuple._ + bifoldMap(fab)((_, B.empty), (A.empty, _)) + } } private[cats] trait ComposedBifoldable[F[_, _], G[_, _]] extends Bifoldable[λ[(α, β) => F[G[α, β], G[α, β]]]] { diff --git a/tests/src/test/scala/cats/tests/BifoldableSuite.scala b/tests/src/test/scala/cats/tests/BifoldableSuite.scala index 2dc2930c69..725035e70a 100644 --- a/tests/src/test/scala/cats/tests/BifoldableSuite.scala +++ b/tests/src/test/scala/cats/tests/BifoldableSuite.scala @@ -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, "")) + } }