Created
March 19, 2014 14:13
-
-
Save gszeliga/9642469 to your computer and use it in GitHub Desktop.
This file contains 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
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