Last active
August 29, 2015 14:08
-
-
Save tixxit/1a0479a64adff05a80ad to your computer and use it in GitHub Desktop.
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
/** | |
* Something similar to Kleisli[Future, A, B], but doesn't require an | |
* ExecutionContext up front. | |
*/ | |
case class AsyncFn[A, B](fn: ExecutionContext => A => Future[B]) { | |
def map[B0](f: B => B0): AsyncFn[A, B0] = | |
AsyncFn[A, B0] { implicit ec => a => | |
fn(ec)(a).map(f) | |
} | |
def flatMap[B0](f: B => AsyncFn[A, B0]): AsyncFn[A, B0] = | |
AsyncFn[A, B0] { implicit ec => a => | |
fn(ec)(a).flatMap { b => | |
f(b).fn(ec)(a) | |
} | |
} | |
def contramap[A0](f: A0 => A): AsyncFn[A0, B] = | |
AsyncFn[A0, B] { implicit ec => a0 => fn(ec)(f(a0)) } | |
def apply(a: A)(implicit ec: ExecutionContext): Future[B] = | |
fn(ec)(a) | |
} | |
object AsyncFn { | |
def lift[A, B](f: A => Future[B]): AsyncFn[A, B] = AsyncFn(_ => f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment