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

Backport bifold in Bifoldable typeclass to scala_2.11 #3242

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ trait AllSyntaxBinCompat5 extends ParallelBitraverseSyntax

trait AllSyntaxBinCompat6 extends ParallelUnorderedTraverseSyntax

trait AllSyntaxBinCompat7 extends FunctorSyntaxBinCompat0
trait AllSyntaxBinCompat7 extends FunctorSyntaxBinCompat0 with BiFoldableSyntaxBinCompat0
25 changes: 25 additions & 0 deletions core/src/main/scala/cats/syntax/bifoldable.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
package cats
package syntax

import scala.language.implicitConversions

trait BifoldableSyntax extends Bifoldable.ToBifoldableOps

private[syntax] trait BiFoldableSyntaxBinCompat0 {
implicit final def catsSyntaxBiFoldOps[F[_, _], A, B](fa: F[A, B]): BifoldBifoldableOps[F, A, B] =
gagandeepkalra marked this conversation as resolved.
Show resolved Hide resolved
new BifoldBifoldableOps[F, A, B](fa)
}

final class BifoldBifoldableOps[F[_, _], A, B](private val fab: F[A, B]) extends AnyVal {
gagandeepkalra marked this conversation as resolved.
Show resolved Hide resolved

/**
* Collapse the structure to a tuple2, given each type has an available [[cats.Monoid]]
*
* {{{
* scala> import cats.implicits._
* scala> Either.left[Int, String](5).bifold == ((5, ""))
* res0: Boolean = true
* }}}
*
*/
def bifold(implicit F: Bifoldable[F], A: Monoid[A], B: Monoid[B]): (A, B) = {
import cats.instances.tuple._
F.bifoldMap(fab)((_, B.empty), (A.empty, _))
}
}
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package object syntax {
object arrow extends ArrowSyntax
object arrowChoice extends ArrowChoiceSyntax
object bifunctor extends BifunctorSyntax
object bifoldable extends BifoldableSyntax
object bifoldable extends BifoldableSyntax with BiFoldableSyntaxBinCompat0
object binested extends BinestedSyntax
object bitraverse extends BitraverseSyntax with BitraverseSyntaxBinCompat0
@deprecated("use cats.syntax.semigroupal instead", "1.0.0-RC1")
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") {
Either.right[Int, String]("something").bifold should ===((0, "something"))
Either.left[Int, String](5).bifold should ===((5, ""))
}
}