Skip to content

Instantly share code, notes, and snippets.

@ryanlecompte
Created October 25, 2013 04:00
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 ryanlecompte/7149289 to your computer and use it in GitHub Desktop.
Save ryanlecompte/7149289 to your computer and use it in GitHub Desktop.
future flatmapping with no stack blowing
scala> def loop(f: Future[Int]): Future[Int] = {
| f.flatMap { i =>
| if (i == 1e6) Future.successful(i)
| else loop(future(i + 1))
| }
| }
scala> val f = loop(future(1))
scala> Await.result(f, Duration.Inf)
res1: Int = 1000000
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment