Skip to content

Instantly share code, notes, and snippets.

@conniec
Last active October 17, 2016 04:36
Show Gist options
  • Save conniec/6491c1d0dc937b66980d96a1ffab6f26 to your computer and use it in GitHub Desktop.
Save conniec/6491c1d0dc937b66980d96a1ffab6f26 to your computer and use it in GitHub Desktop.
import cats.data.EitherT
import cats.data.Validated
import cats.implicits._
// def ap[A, B](f: F[A => B])(fa: F[A]): F[B]
val l1: EitherT[Option, String, Int] = EitherT.left(Some("error 1"))
val r1: EitherT[Option, String, Int => String] = EitherT.right(Some(f))
val r2: EitherT[Option, String, Int] = EitherT.right(Some(10))
val f: Int => String = i => (i * 2).toString
type ValidatedOr[A] = Validated[String, A]
type OptionErrorOr[A] = Nested[Option, ValidatedOr, A]
// This will map the function f on the EitherT.left, but it converts to Invalid so nothing happens
val nested = (r1.toNestedValidated: OptionErrorOr[Int => String]).ap(l1.toNestedValidated: OptionErrorOr[Int])
// nested: OptionErrorOr[String] = Nested(Some(Invalid(error 1)))
// This will map the function f on the EitherT.right that converts to a Valid
val nested2 = (r1.toNestedValidated: OptionErrorOr[Int => String]).ap(r2.toNestedValidated: OptionErrorOr[Int])
// nested2: OptionErrorOr[String] = Nested(Some(Valid(20)))
/**
* Trying to do the same ap() with .toValidated() without Nested, doesn't work
* scala> type OptionValidated[A] = Option[Validated[String, A]]
* defined type alias OptionValidated
* scala> (r1.toValidated: OptionValidated[Int => String]).ap(l1.toValidated: OptionValidated[Int])
* <console>:23: error: value ap is not a member of OptionValidated[Int => String]
* (r1.toValidated: OptionValidated[Int => String]).ap(l1.toValidated: OptionValidated[Int])
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment