Skip to content

Commit

Permalink
Add mergeLeft and mergeRight to Ior
Browse files Browse the repository at this point in the history
Add put operators, make ev.apply explicit

Add putLeft and putRight tests

Use ev directly instead of eta-expanding

More polymorphism, thanks johnynek
  • Loading branch information
edmundnoble authored and Edmund Noble committed Jan 3, 2017
1 parent 730d521 commit 6735c43
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ sealed abstract class Ior[+A, +B] extends Product with Serializable {
case Ior.Both(a, b) => fab(a, b)
}

final def putLeft[C](left: C): C Ior B =
fold(_ => Ior.left(left), Ior.both(left, _), (_, b) => Ior.both(left, b))
final def putRight[C](right: C): A Ior C =
fold(Ior.both(_, right), _ => Ior.right(right), (a, _) => Ior.both(a, right))

final def isLeft: Boolean = fold(_ => true, _ => false, (_, _) => false)
final def isRight: Boolean = fold(_ => false, _ => true, (_, _) => false)
final def isBoth: Boolean = fold(_ => false, _ => false, (_, _) => true)
Expand Down Expand Up @@ -90,7 +95,11 @@ sealed abstract class Ior[+A, +B] extends Product with Serializable {
fold(_ => lc, f(_, lc), (_, b) => f(b, lc))

final def merge[AA >: A](implicit ev: B <:< AA, AA: Semigroup[AA]): AA =
fold(identity, ev.apply, (a, b) => AA.combine(a, b))
fold(identity, ev, (a, b) => AA.combine(a, b))
final def mergeLeft[AA >: A](implicit ev: B <:< AA): AA =
fold(identity, ev, (a, _) => a)
final def mergeRight[AA >: A](implicit ev: B <:< AA): AA =
fold(identity, ev, (_, b) => ev(b))

// scalastyle:off cyclomatic.complexity
final def append[AA >: A, BB >: B](that: AA Ior BB)(implicit AA: Semigroup[AA], BB: Semigroup[BB]): AA Ior BB = this match {
Expand Down
41 changes: 41 additions & 0 deletions tests/src/test/scala/cats/tests/IorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,47 @@ class IorTests extends CatsSuite {
}
}


test("merge") {
forAll { (i: Int Ior Int) =>
i.merge should === (i.left.getOrElse(0) + i.right.getOrElse(0))
}
}

test("mergeLeft") {
forAll { (i: Int Ior Int) =>
i.mergeLeft should === (i.left.orElse(i.right).get)
}
}

test("mergeRight") {
forAll { (i: Int Ior Int) =>
i.mergeRight should === (i.right.orElse(i.left).get)
}
}

test("putLeft") {
forAll { (i: Int Ior Int) =>
val expectedResult =
if (i.isLeft)
Ior.left(2)
else
Ior.both(2, i.right.get)
i.putLeft(2) should === (expectedResult)
}
}

test("putRight") {
forAll { (i: Int Ior Int) =>
val expectedResult =
if (i.isRight)
Ior.right(2)
else
Ior.both(i.left.get, 2)
i.putRight(2) should === (expectedResult)
}
}

test("append left") {
forAll { (i: Int Ior String, j: Int Ior String) =>
i.append(j).left should === (i.left.map(_ + j.left.getOrElse(0)).orElse(j.left))
Expand Down

0 comments on commit 6735c43

Please sign in to comment.