Skip to content

Instantly share code, notes, and snippets.

@gszeliga
Created March 19, 2014 14:13
Show Gist options
  • Save gszeliga/9642469 to your computer and use it in GitHub Desktop.
Save gszeliga/9642469 to your computer and use it in GitHub Desktop.
trait Future[+A] {
private def apply(k: A => Unit): Unit
}
type Par[+A] = ExecutorService => Future[A]
def map2[A, B, C](pa: Par[A], pb: Par[B])(f: (A, B) => C): Par[C] = {
ex =>
{
new Future[C] {
def apply(k: C => Unit) = {
var va: Option[A] = None
var vb: Option[B] = None
val actor = Actor[Either[A, B]](ex) {
case Left(v) => {
if (vb.isDefined) k(f(v, vb.get))
else va = Some(v)
}
case Right(v) => {
if (va.isDefined) k(f(va.get, v))
else vb = Some(v)
}
}
pa(ex) { v => actor ! Left(v) }
pb(ex) { v => actor ! Right(v) }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment