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 Ior.fromEither #2057

Merged
merged 2 commits into from
Dec 1, 2017
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
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

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