Skip to content

Instantly share code, notes, and snippets.

@rjsvaljean
Created July 14, 2017 07:28
Show Gist options
  • Select an option

  • Save rjsvaljean/a285daf5ec24f6795a7ffa1a60722447 to your computer and use it in GitHub Desktop.

Select an option

Save rjsvaljean/a285daf5ec24f6795a7ffa1a60722447 to your computer and use it in GitHub Desktop.
case class ValidatedT[F[_], +E, +A](run: F[Validated[E, A]])
import cats.Monad
import cats.syntax.flatMap._
object ValidatedT {
implicit def monad[F[_]: Monad, E]: Monad[({type l[a] = ValidatedT[F, E, a]})#l] = new Monad[({type l[a] = ValidatedT[F, E, a]})#l] {
def pure[A](x: A) = ValidatedT(Monad[F].pure(Validated.valid(x)))
def flatMap[A, B](fa: ValidatedT[F, E, A])(f: (A) => ValidatedT[F, E, B]) =
ValidatedT(fa.run.flatMap(_.fold(e => Monad[F].pure(Validated.invalid[E, B](e)), i => f(i).run)))
def tailRecM[A, B](a: A)(f: (A) => ValidatedT[F, E, Either[A, B]]) = ???
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment