Skip to content

Commit

Permalink
backported typelevel#3088 (adding bifold to Bifoldable typeclass)…
Browse files Browse the repository at this point in the history
… to scala_2.11
  • Loading branch information
gagandeepkalra committed Jan 9, 2020
1 parent 7890320 commit c086d87
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
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] =
new BifoldBifoldableOps[F, A, B](fa)
}

final class BifoldBifoldableOps[F[_, _], A, B](private val fab: F[A, B]) extends AnyVal {

/**
* 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, ""))
}
}

0 comments on commit c086d87

Please sign in to comment.