You can translate your example into Scala like this:
import concurrent.{Await, Future, future} import concurrent.ExecutionContext.Implicits.global import concurrent.duration.Duration object Main extends App { val fac: Int => Int = { case 0 => 1 case n => n * fac(n-1) } val ack: (Int, Int) => Int = { case (0, n) => n + 1 case (m, 0) => ack (m-1, 1) case (m, n) => ack (m-1, ack(m, n-1)) } val fib: Int => Int = { case 0 => 0 case 1 => 1 case n => fib(n-1) + fib(n-2) } val fa = future { ack(3, 10) } val fb = future { fac(42) } val fc = future { fib(35) } val x = for (((a, b), c) <- fa zip fb zip fc) yield (a + b + c) val result = Await.result(x, Duration.Inf)
The future { fib(3, 10) } bit will create an asynchronous calculation that will be executed in another thread of execution and will return a Future object. You can then put all your futures into one big future that will provide a list of all the results using Future.sequence .
We can map the result of this last future into the sum of the results, thereby obtaining the final value.
In this ultimate future, we can do several things. We can put it together or attach callbacks to it, or we can wait synchronously for a certain period of time. In my example, I wait synchronously after the result for an infinite period of time.
source share