Skip to content

Instantly share code, notes, and snippets.

@ekmett
Created December 28, 2010 07:58
Show Gist options
  • Save ekmett/757030 to your computer and use it in GitHub Desktop.
Save ekmett/757030 to your computer and use it in GitHub Desktop.
a sketch of a scala speculation framework
package speculation
import java.util.concurrent.{ Callable, ExecutorService, Future }
class Speculation(val executor: ExecutorService, val mayInterruptIfRunning: Boolean) {
def spec[A,B](guess: => A)(f: A => B)(actual: => A): B = {
val g = executor.submit(new Callable[A] {
def call = guess
})
val speculation = executor.submit(new Callable[B] {
def call = f(g.get())
})
val a = actual
val guessed = g.get()
if (a == guessed) {
speculation.get()
} else {
speculation.cancel(mayInterruptIfRunning)
f(actual)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment