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
Changes from 1 commit
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
Next Next commit
Add Ior.fromEither
markus1189 committed Dec 1, 2017

Verified

This commit was signed with the committer’s verified signature.
gsherwood Greg Sherwood
commit 7a2fb65eb7f08a24dee016d403ac5ae7c609d462
21 changes: 21 additions & 0 deletions core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
@@ -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
@@ -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