Created
November 6, 2011 14:10
-
-
Save heathermiller/1342925 to your computer and use it in GitHub Desktop.
Clojure to Scala, cleaned up
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
def slow = Thread.sleep(700) | |
def fast = Thread.sleep(100) | |
def threadDeliver[T](fun: => T, prom: SyncVar[T]) = | |
actor { prom set fun } | |
def tryDeliver[T](promise: SyncVar[T], a: => T, timeout: Long)(otherwise: => T): T = { | |
threadDeliver(a, promise) | |
promise.get(timeout) match { | |
case None => otherwise | |
case Some(result) => result | |
} | |
} | |
def main[T](a: => T, aw: Long, b: => T, overall: Long, default: T): T = { | |
val promise = new SyncVar[T] | |
tryDeliver(promise, a, aw) { | |
tryDeliver(promise, b, overall - aw) { default } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment