Skip to content

Commit

Permalink
Add Ior.fromEither (#2057)
Browse files Browse the repository at this point in the history
* Add `Ior.fromEither`

* Make binary compat exclusion
  • Loading branch information
markus1189 authored and kailuowang committed Dec 1, 2017
1 parent e431b72 commit f29dc72
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,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")

Expand Down
21 changes: 21 additions & 0 deletions core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 1 addition & 4 deletions core/src/main/scala/cats/syntax/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f29dc72

Please sign in to comment.