Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wanted to try out the new <a href="https://github.com/scala/async" rel="nofollow">Async API</a> and this was a nice test. In fact there is an example that is very close to your use case on the Async Github home page.</p> <p>Async is a <a href="http://docs.scala-lang.org/sips/pending/async.html" rel="nofollow">SIP</a> and will likely be part of standard Scala at some point. </p> <p>Besides using <code>await</code> the idea here is that you have an abstract <code>add()</code> method that uses asynchronous logic behind the scenes. That way it is hidden from the <code>Calculator</code> developers. </p> <p>Personally I would add an async version of the API too so <code>Future</code>s could be passed out of the <code>Calculator</code> to be composed with other <code>Future</code>s.</p> <pre><code>trait Calculator { def hardToCalculate1: Int def hardToCalculate2: Int def hardToCalculate3: Int def add(a: =&gt; Int, b: =&gt; Int): Int def result1 = add(hardToCalculate1, hardToCalculate2) def result2 = add(hardToCalculate2, hardToCalculate3) def result3 = add(hardToCalculate1, hardToCalculate3) } object So17677728 extends App with Calculator { override def add(a: =&gt; Int, b: =&gt; Int): Int = { val start = System.currentTimeMillis val res = Await.result(asyncAdd(a, b), 2000 millis) val end = System.currentTimeMillis println(s"Total time: ${end - start} ms") res } def asyncAdd(a: =&gt; Int, b: =&gt; Int): Future[Int] = { async { val fa = Future(a) val fb = Future(b) await(fa) + await(fb) } } val random = Random val maxSleep = 1000 def hardToCalculate1: Int = htc(1) def hardToCalculate2: Int = htc(2) def hardToCalculate3: Int = htc(3) def htc(n: Int) = { val sleepMs = random.nextInt(1000) println(s"$n sleeping for $sleepMs ms") Thread.sleep(sleepMs) println(s"$n done sleeping") n } println(s"result1: $result1\n") println(s"result2: $result2\n") println(s"result3: $result3\n") } </code></pre> <p>Output</p> <pre><code>1 sleeping for 438 ms 2 sleeping for 244 ms 2 done sleeping 1 done sleeping Total time: 497 ms result1: 3 3 sleeping for 793 ms 2 sleeping for 842 ms 3 done sleeping 2 done sleeping Total time: 844 ms result2: 5 3 sleeping for 895 ms 1 sleeping for 212 ms 1 done sleeping 3 done sleeping Total time: 896 ms result3: 4 </code></pre> <p>Alternatively in <code>add</code> you could <code>Future</code>s and a for comprehension instead of <code>async</code>/<code>await</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload