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

Add addLeft/addRight for Ior #3698

Merged
merged 1 commit into from
Dec 6, 2020
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
52 changes: 52 additions & 0 deletions core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,58 @@ sealed abstract class Ior[+A, +B] extends Product with Serializable {
final def putRight[C](right: C): A Ior C =
fold(Ior.both(_, right), _ => Ior.right(right), (a, _) => Ior.both(a, right))

/**
* When a Left value is present in the Ior, combine it will the value specified.
*
* When the Left value is absent, set it to the value specified.
*
* Example:
* {{{
* scala> import cats.data.Ior
* scala> import cats.implicits._
*
* scala> val ior1 = "abc".leftIor[Int]
* scala> ior1.addLeft("def")
* res0: Ior[String, Int] = Left(abcdef)
*
* scala> val ior2 = 123.rightIor[String]
* scala> ior2.addLeft("abc")
* res1: cats.data.Ior[String,Int] = Both(abc,123)
*
* scala> val ior3 = Ior.Both("abc",123)
* scala> ior3.addLeft("def")
* res2: Ior[String, Int] = Both(abcdef,123)
* }}}
*/
final def addLeft[AA >: A](left: AA)(implicit AA: Semigroup[AA]): AA Ior B =
fold(l => Ior.left(AA.combine(l, left)), Ior.both(left, _), (l, r) => Ior.both(AA.combine(l, left), r))

/**
* When a Right value is present in the Ior, combine it will the value specified.
*
* When the Right value is absent, set it to the value specified.
*
* Example:
* {{{
* scala> import cats.data.Ior
* scala> import cats.implicits._
*
* scala> val ior1 = "abc".leftIor[Int]
* scala> ior1.addRight(123)
* res0: Ior[String, Int] = Both(abc,123)
*
* scala> val ior2 = 123.rightIor[String]
* scala> ior2.addRight(123)
* res1: Ior[String, Int] = Right(246)
*
* scala> val ior3 = Ior.Both("abc",123)
* scala> ior3.addRight(123)
* res2: Ior[String, Int] = Both(abc,246)
* }}}
*/
final def addRight[BB >: B](right: BB)(implicit BB: Semigroup[BB]): A Ior BB =
fold(Ior.both(_, right), r => Ior.right(BB.combine(r, right)), (l, r) => Ior.both(l, BB.combine(r, right)))

/**
* Example:
* {{{
Expand Down
26 changes: 26 additions & 0 deletions tests/src/test/scala/cats/tests/IorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,32 @@ class IorSuite extends CatsSuite {
}
}

test("addLeft") {
forAll { (i: Int Ior Int, j: Int) =>
val expectedResult =
if (i.isLeft)
Ior.left(i.left.get + j)
else if (i.isRight)
Ior.both(j, i.right.get)
else
Ior.both(i.left.get + j, i.right.get)
assert(i.addLeft(j) === expectedResult)
}
}

test("addRight") {
forAll { (i: Int Ior Int, j: Int) =>
val expectedResult =
if (i.isLeft)
Ior.both(i.left.get, j)
else if (i.isRight)
Ior.right(i.right.get + j)
else
Ior.both(i.left.get, i.right.get + j)
assert(i.addRight(j) === expectedResult)
}
}

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