Created
          December 28, 2010 07:58 
        
      - 
      
- 
        Save ekmett/757030 to your computer and use it in GitHub Desktop. 
    a sketch of a scala speculation framework
  
        
  
    
      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
    
  
  
    
  | 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