Skip to content

Commit

Permalink
raiseOrPure (#1933)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpolecat authored and peterneyens committed Sep 26, 2017
1 parent dc8637d commit 9123be0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/syntax/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ final class EitherOps[A, B](val eab: Either[A, B]) extends AnyVal {
* }}}
*/
def toEitherT[F[_]: Applicative]: EitherT[F, A, B] = EitherT.fromEither(eab)

def raiseOrPure[F[_]](implicit ev: ApplicativeError[F, A]): F[B] =
ev.fromEither(eab)

}

final class EitherObjectOps(val either: Either.type) extends AnyVal { // scalastyle:off ensure.single.space.after.token
Expand Down
9 changes: 8 additions & 1 deletion tests/src/test/scala/cats/tests/EitherTests.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package tests

import cats.data.EitherT
import cats.data.{ EitherT, Validated }
import cats.laws.discipline._
import cats.kernel.laws.{GroupLaws, OrderLaws}
import scala.util.Try
Expand Down Expand Up @@ -276,4 +276,11 @@ class EitherTests extends CatsSuite {
}
}

test("raiseOrPure syntax consistent with fromEither") {
val ev = ApplicativeError[Validated[String, ?], String]
forAll { (fa: Either[String, Int]) =>
fa.raiseOrPure[Validated[String, ?]] should === (ev.fromEither(fa))
}
}

}

0 comments on commit 9123be0

Please sign in to comment.