Last active
          June 12, 2018 07:04 
        
      - 
      
- 
        Save afsalthaj/ddfe60c06fb60eec864e0e4364f1911a to your computer and use it in GitHub Desktop. 
  
    
      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
    
  
  
    
  | // Time to scrap Future! | |
| // The **push** nature of scala.concurrent.Future! | |
| val s = Future { (0 to 10).map(t => Thread.sleep(2000); scala.util.Random.nextInt(1 + t)) } | |
| s.foreach(println(_)) | |
| // It will take 20 seconds to print out Vector(0, 0, 0, 1, 0, 4, 1, 3, 5, 9, 10) | |
| s.foreach(println(_)) | |
| // This time the same line of code behaves differently. | |
| // It prints out without delay and hence broke the substitution principle. | |
| // The cool *pull* nature of **scalaz**.concurrent.Future though not popular as IO | |
| val s = scalaz.concurrent.Future { (0 to 10).map(t => {Thread.sleep(1000); scala.util.Random.nextInt(1 + t) }) } | |
| s.map(println(_)).unsafePerformSync | |
| s.map(println(_)).unsafePerformSync | |
| // Vector(0, 1, 2, 1, 1, 5, 0, 0, 3, 0, 1) - takes 10 seconds | |
| // Vector(0, 1, 2, 3, 4, 2, 1, 6, 8, 3, 10) - takes 10 seconds | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment