Skip to content

Instantly share code, notes, and snippets.

@heathermiller
Created November 6, 2011 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathermiller/1342925 to your computer and use it in GitHub Desktop.
Save heathermiller/1342925 to your computer and use it in GitHub Desktop.
Clojure to Scala, cleaned up
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