From 7a2fb65eb7f08a24dee016d403ac5ae7c609d462 Mon Sep 17 00:00:00 2001 From: Markus Hauck Date: Fri, 1 Dec 2017 09:41:48 +0100 Subject: [PATCH 1/2] Add `Ior.fromEither` --- core/src/main/scala/cats/data/Ior.scala | 21 ++++++++++++++++++++ core/src/main/scala/cats/syntax/either.scala | 5 +---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/core/src/main/scala/cats/data/Ior.scala b/core/src/main/scala/cats/data/Ior.scala index 45b5edebac..1c458ff5c1 100644 --- a/core/src/main/scala/cats/data/Ior.scala +++ b/core/src/main/scala/cats/data/Ior.scala @@ -257,4 +257,25 @@ private[data] sealed trait IorFunctions { case None => None } } + + /** + * Create an `Ior` from an `Either`. + * @param eab an `Either` from which the `Ior` should be created + * + * @return [[Ior.Left]] if the `Either` was a `Left`, + * or [[Ior.Right]] if the `Either` was a `Right` + * + * Example: + * {{{ + * scala> Ior.fromEither(Left(1)) + * res0: Ior[Int, Nothing] = Left(1) + * scala> Ior.fromEither(Right('1')) + * res1: Ior[Nothing, Char] = Right(1) + * }}} + */ + def fromEither[A, B](eab: Either[A, B]): A Ior B = + eab match { + case Left(a) => left(a) + case Right(b) => right(b) + } } diff --git a/core/src/main/scala/cats/syntax/either.scala b/core/src/main/scala/cats/syntax/either.scala index d435dd0c9d..bad47ec411 100644 --- a/core/src/main/scala/cats/syntax/either.scala +++ b/core/src/main/scala/cats/syntax/either.scala @@ -85,10 +85,7 @@ final class EitherOps[A, B](val eab: Either[A, B]) extends AnyVal { case Right(b) => if (f(b)) eab else Left(onFailure(b)) } - def toIor: A Ior B = eab match { - case Left(a) => Ior.left(a) - case Right(b) => Ior.right(b) - } + def toIor: A Ior B = Ior.fromEither(eab) def toOption: Option[B] = eab match { case Left(_) => None From 42fc926f0819b54e2f06b77918c61970457ffc20 Mon Sep 17 00:00:00 2001 From: LukaJCB Date: Fri, 1 Dec 2017 13:30:28 +0100 Subject: [PATCH 2/2] Make binary compat exclusion --- build.sbt | 1 + 1 file changed, 1 insertion(+) diff --git a/build.sbt b/build.sbt index f88c72b2eb..5f790e4d54 100644 --- a/build.sbt +++ b/build.sbt @@ -275,6 +275,7 @@ def mimaSettings(moduleName: String) = Seq( exclude[DirectMissingMethodProblem]("cats.data.NestedApplicativeError.sequence"), exclude[DirectMissingMethodProblem]("cats.data.ValidatedApplicative.traverse"), exclude[DirectMissingMethodProblem]("cats.data.ValidatedApplicative.sequence"), + exclude[ReversedMissingMethodProblem]("cats.data.IorFunctions.fromEither"), exclude[DirectMissingMethodProblem]("cats.data.RWSTAlternative.traverse"), exclude[DirectMissingMethodProblem]("cats.data.RWSTAlternative.sequence")