Skip to content

Commit

Permalink
#2947 Added bifold to Bifoldable typeclass (#3088)
Browse files Browse the repository at this point in the history
* #2947 Added bifold to Bifoldable typeclass

* #2947 Formatted

* address Luka's feedback

* reformat
  • Loading branch information
Twizty authored and LukaJCB committed Nov 5, 2019
1 parent 6d191a5 commit 5ad3c40
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 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,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[α, β]]]] {
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 5ad3c40

Please sign in to comment.