Skip to content

Instantly share code, notes, and snippets.

@hisui
Created August 5, 2013 01:32
Show Gist options
  • Save hisui/6152881 to your computer and use it in GitHub Desktop.
Save hisui/6152881 to your computer and use it in GitHub Desktop.
Enables Play2's FunctionalBuilder for Scala10's Try[A].
import play.api.libs.functional._
import scala.language.higherKinds
import scala.util.{Success, Try}
object Play2FunctionalImplicitConversions {
implicit val tryApplicative:Applicative[Try] =
new Applicative[Try] {
def apply[A, B](mf: Try[(A) => B], ma: Try[A]): Try[B] = mf.flatMap(f => ma.map(f(_)))
def pure[A](a: A): Try[A] = Success(a)
def map[A, B](m: Try[A], f: (A) => B): Try[B] = m.map(f)
}
implicit val tryVariant:Variant[Try] = new Variant[Try] {}
implicit def applicativeToFunctor[M[_]](implicit ev:Applicative[M]):Functor[M] = new Functor[M] {
def fmap[A, B](m: M[A], f: (A) => B): M[B] = ev.map(m, f)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment