Skip to content

Instantly share code, notes, and snippets.

@YoEight
Created July 11, 2012 18:26
Show Gist options
  • Save YoEight/3092181 to your computer and use it in GitHub Desktop.
Save YoEight/3092181 to your computer and use it in GitHub Desktop.
Akka Future
//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]
}
@ahoy-jon
Copy link

Ho, I didn't see it like that, with your FutureW.

@YoEight
Copy link
Author

YoEight commented Jul 11, 2012

Does it meet your need ?

@ahoy-jon
Copy link

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