Skip to content

Instantly share code, notes, and snippets.

@afsalthaj
Last active June 12, 2018 07:04
Show Gist options
  • Save afsalthaj/ddfe60c06fb60eec864e0e4364f1911a to your computer and use it in GitHub Desktop.
Save afsalthaj/ddfe60c06fb60eec864e0e4364f1911a to your computer and use it in GitHub Desktop.
// 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