Skip to content

Commit

Permalink
Added Bitraverse for Ior (#2144)
Browse files Browse the repository at this point in the history
  • Loading branch information
V-Lamp committed Jan 23, 2018
1 parent dbac00d commit cc411db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ object Ior extends IorInstances with IorFunctions {
}

private[data] sealed abstract class IorInstances extends IorInstances0 {

implicit val catsBitraverseForIor: Bitraverse[Ior] = new Bitraverse[Ior] {

def bitraverse[G[_], A, B, C, D](fab: Ior[A, B])(f: A => G[C], g: B => G[D])(implicit G: Applicative[G]): G[Ior[C, D]] =
fab.fold(
a => G.map(f(a))(Ior.Left(_)),
b => G.map(g(b))(Ior.Right(_)),
(a, b) => G.map2(f(a), g(b))(Ior.Both(_, _))
)

def bifoldLeft[A, B, C](fab: Ior[A, B], c: C)(f: (C, A) => C, g: (C, B) => C): C =
fab.fold(f(c, _), g(c, _), (a, b) => g(f(c, a), b))

def bifoldRight[A, B, C](fab: Ior[A, B], c: Eval[C])(f: (A, Eval[C]) => Eval[C], g: (B, Eval[C]) => Eval[C]): Eval[C] =
fab.fold(f(_, c), g(_, c), (a, b) => g(b, f(a, c)))
}

implicit def catsDataEqForIor[A: Eq, B: Eq]: Eq[A Ior B] = new Eq[A Ior B] {
def eqv(x: A Ior B, y: A Ior B): Boolean = x === y
}
Expand Down
3 changes: 3 additions & 0 deletions tests/src/test/scala/cats/tests/IorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class IorSuite extends CatsSuite {
checkAll("Traverse[String Ior ?]", SerializableTests.serializable(Traverse[String Ior ?]))
checkAll("? Ior ?", BifunctorTests[Ior].bifunctor[Int, Int, Int, String, String, String])

checkAll("Ior[?, ?]", BitraverseTests[Ior].bitraverse[Option, Int, Int, Int, String, String, String])
checkAll("Bitraverse[Ior]", SerializableTests.serializable(Bitraverse[Ior]))

checkAll("Semigroup[Ior[A: Semigroup, B: Semigroup]]", SemigroupTests[Ior[List[Int], List[Int]]].semigroup)
checkAll("SerializableTest Semigroup[Ior[A: Semigroup, B: Semigroup]]", SerializableTests.serializable(Semigroup[Ior[List[Int], List[Int]]]))

Expand Down

0 comments on commit cc411db

Please sign in to comment.