Created
July 11, 2012 18:26
-
-
Save YoEight/3092181 to your computer and use it in GitHub Desktop.
Akka Future
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//assuming all akka imports | |
object FutureTest { | |
private case class FutureW[A](proc: ExecutionContext => Future[A]) { // note the similarity with Kleisli[Future, ExecutionContext, A] | |
implicit val defaultDuration = Duration("3s") | |
def map[B](f: A => B) = FutureW(c => proc(c).map(f)) | |
def flatMap[B](f: A => FutureW[B]) = FutureW(c => proc(c).flatMap(a => f(a).apply(c))) | |
} | |
implicit val futureWMonad = new Monad[FutureW]{ | |
def point[A](v: => A) = FutureW(c => Promise.successful(a)(c)) | |
def bind[A, B](fa: FutureW[A])(f: A => FutureW[B]) = fa flatMap f | |
} | |
implicit def toFutureW[A](v: Future[A]): FutureW[A] = FutureW[A](_ => v) | |
// Since you have a Monad[FutureW] you can use Validation monad transformer (require scalaz-seven) | |
type FutureValidation[E, A] = ValidationT[FutureW, E, A] | |
} |
Does it meet your need ?
I don't now yet ! Anyways, that's interesting.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ho, I didn't see it like that, with your FutureW.