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

Clean up EitherT doctests #1693

Merged
merged 1 commit into from
May 25, 2017
Merged
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
46 changes: 24 additions & 22 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) {
* {{{
* scala> import cats.implicits._
* scala> type Error = String
* scala> val v1: Validated[NonEmptyList[Error], Int] = Validated.Invalid(NonEmptyList.of("error 1"))
* scala> val v2: Validated[NonEmptyList[Error], Int] = Validated.Invalid(NonEmptyList.of("error 2"))
* scala> val eithert: EitherT[Option, Error, Int] = EitherT(Some(Either.left("error 3")))
* scala> eithert.withValidated { v3 => (v1 |@| v2 |@| v3.leftMap(NonEmptyList.of(_))).map{ case (i, j, k) => i + j + k } }
* scala> val v1: Validated[NonEmptyList[Error], Int] = Validated.invalidNel("error 1")
* scala> val v2: Validated[NonEmptyList[Error], Int] = Validated.invalidNel("error 2")
* scala> val eithert: EitherT[Option, Error, Int] = EitherT.leftT[Option, Int]("error 3")
* scala> eithert.withValidated { v3 => (v1 |@| v2 |@| v3.toValidatedNel).map { case (i, j, k) => i + j + k } }
* res0: EitherT[Option, NonEmptyList[Error], Int] = EitherT(Some(Left(NonEmptyList(error 1, error 2, error 3))))
* }}}
*/
Expand All @@ -192,42 +192,44 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) {
* inconsistent with the behavior of the `ap` from `Monad` of `EitherT`.
*
* {{{
* scala> import cats.data.{Nested, EitherT}
* scala> import cats.data.EitherT
* scala> import cats.implicits._
* scala> val ff: EitherT[List, String, Int => String] =
* | EitherT(List(Either.right(_.toString), Either.left("error")))
* scala> val fa: EitherT[List, String, Int] =
* | EitherT(List(Either.right(1), Either.right(2)))
* scala> type ListErrorOr[A] = Nested[List, Either[String, ?], A]
* scala> ff.ap(fa)
* res0: EitherT[List,String,String] = EitherT(List(Right(1), Right(2), Left(error)))
* scala> EitherT((ff.toNested: ListErrorOr[Int => String]).ap(fa.toNested: ListErrorOr[Int]).value)
* scala> EitherT((ff.toNested).ap(fa.toNested).value)
* res1: EitherT[List,String,String] = EitherT(List(Right(1), Right(2), Left(error), Left(error)))
* }}}
*
*/
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]`.
* Transform this `EitherT[F, A, B]` into a `[[Nested]][F, Validated[A, ?], B]`.
*
* Example:
* {{{
* scala> import cats.data.{Validated, EitherT, Nested}
* scala> import cats.data.{EitherT, Validated}
* 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>)))
* 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)))
* r2: cats.data.EitherT[Option,String,Int] = EitherT(Some(Right(10)))
* scala> type ErrorOr[A] = Validated[String, A]
* scala> (r1.toNestedValidated).ap(r2.toNestedValidated)
* res0: cats.data.Nested[Option,ErrorOr,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))

/**
* Transform this `EitherT[F, A, B]` into a `[[Nested]][F, ValidatedNel[A, ?], B]`.
*/
def toNestedValidatedNel(implicit F: Functor[F]): Nested[F, ValidatedNel[A, ?], B] =
Nested[F, ValidatedNel[A, ?], B](F.map(value)(_.toValidatedNel))
}
Expand All @@ -237,7 +239,7 @@ object EitherT extends EitherTInstances {
/**
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
private[data] final class LeftPartiallyApplied[B](val dummy: Boolean = true ) extends AnyVal {
private[data] final class LeftPartiallyApplied[B](val dummy: Boolean = true) extends AnyVal {
def apply[F[_], A](fa: F[A])(implicit F: Functor[F]): EitherT[F, A, B] = EitherT(F.map(fa)(Either.left))
}

Expand All @@ -255,7 +257,7 @@ object EitherT extends EitherTInstances {
/**
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
private[data] final class LeftTPartiallyApplied[F[_], B](val dummy: Boolean = true ) extends AnyVal {
private[data] final class LeftTPartiallyApplied[F[_], B](val dummy: Boolean = true) extends AnyVal {
def apply[A](a: A)(implicit F: Applicative[F]): EitherT[F, A, B] = EitherT(F.pure(Either.left(a)))
}

Expand All @@ -273,7 +275,7 @@ object EitherT extends EitherTInstances {
/**
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
private[data] final class RightPartiallyApplied[A](val dummy: Boolean = true ) extends AnyVal {
private[data] final class RightPartiallyApplied[A](val dummy: Boolean = true) extends AnyVal {
def apply[F[_], B](fb: F[B])(implicit F: Functor[F]): EitherT[F, A, B] = EitherT(F.map(fb)(Either.right))
}

Expand All @@ -291,7 +293,7 @@ object EitherT extends EitherTInstances {
/**
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
private[data] final class PurePartiallyApplied[F[_], A](val dummy: Boolean = true ) extends AnyVal {
private[data] final class PurePartiallyApplied[F[_], A](val dummy: Boolean = true) extends AnyVal {
def apply[B](b: B)(implicit F: Applicative[F]): EitherT[F, A, B] = right(F.pure(b))
}

Expand Down Expand Up @@ -351,7 +353,7 @@ object EitherT extends EitherTInstances {
/**
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
private[data] final class FromEitherPartiallyApplied[F[_]](val dummy: Boolean = true ) extends AnyVal {
private[data] final class FromEitherPartiallyApplied[F[_]](val dummy: Boolean = true) extends AnyVal {
def apply[E, A](either: Either[E, A])(implicit F: Applicative[F]): EitherT[F, E, A] =
EitherT(F.pure(either))
}
Expand All @@ -372,7 +374,7 @@ object EitherT extends EitherTInstances {
/**
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
private[data] final class FromOptionPartiallyApplied[F[_]](val dummy: Boolean = true ) extends AnyVal {
private[data] final class FromOptionPartiallyApplied[F[_]](val dummy: Boolean = true) extends AnyVal {
def apply[E, A](opt: Option[A], ifNone: => E)(implicit F: Applicative[F]): EitherT[F, E, A] =
EitherT(F.pure(Either.fromOption(opt, ifNone)))
}
Expand Down Expand Up @@ -410,7 +412,7 @@ object EitherT extends EitherTInstances {
/**
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
private[data] final class CondPartiallyApplied[F[_]](val dummy: Boolean = true ) extends AnyVal {
private[data] final class CondPartiallyApplied[F[_]](val dummy: Boolean = true) extends AnyVal {
def apply[E, A](test: Boolean, right: => A, left: => E)(implicit F: Applicative[F]): EitherT[F, E, A] =
EitherT(F.pure(Either.cond(test, right, left)))
}
Expand Down