Skip to content

Commit

Permalink
Add example for using toNestedValidated
Browse files Browse the repository at this point in the history
  • Loading branch information
conniec committed Oct 21, 2016
1 parent 0638e65 commit 3187feb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) {
*/
def toNested: Nested[F, Either[A, ?], B] = Nested[F, Either[A, ?], B](value)

/**
* Transform this `EitherT[F, A, B]` into a `[[Nested]][F, Validated[A, ?], B]` or `[[Nested]][F, ValidatedNel[A, B]`.
* Example:
* {{{
* scala> import cats.data.{Validated, EitherT, Nested}
* scala> import cats.implicits._
* scala> val f: Int => String = i => (i*2).toString
* scala> val r1: EitherT[Option, String, Int => String] = EitherT.right(Some(f))
* | r1: cats.data.EitherT[Option,String,Int => String] = EitherT(Some(Right(<function1>)))
* scala> val r2: EitherT[Option, String, Int] = EitherT.right(Some(10))
* | r2: cats.data.EitherT[Option,String,Int] = EitherT(Some(Right(10)))
* scala> type ValidatedOr[A] = Validated[String, A]
* scala> type OptionErrorOr[A] = Nested[Option, ValidatedOr, A]
* scala> (r1.toNestedValidated: OptionErrorOr[Int => String]).ap(r2.toNestedValidated: OptionErrorOr[Int])
* res0: OptionErrorOr[String] = Nested(Some(Valid(20)))
* }}}
*/
def toNestedValidated(implicit F: Functor[F]): Nested[F, Validated[A, ?], B] =
Nested[F, Validated[A, ?], B](F.map(value)(_.toValidated))

Expand Down

0 comments on commit 3187feb

Please sign in to comment.