Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2947 Added bifold to Bifoldable typeclass #3088

Merged
merged 4 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, ""))
}
}