Simple Scala actor question

后端 未结 2 681
粉色の甜心
粉色の甜心 2021-02-06 04:53

I\'m sure this is a very simple question, but embarrassed to say I can\'t get my head around it:

I have a list of values in Scala. I would like to use use actors to make

2条回答
  •  终归单人心
    2021-02-06 05:24

    There's an actor-using class in Scala that's made precisely for this kind of problem: Futures. This problem would be solved like this:

    // This assigns futures that will execute in parallel
    // In the example, the computation is performed by the "process" function
    val tasks = list map (value => scala.actors.Futures.future { process(value) })
    
    // The result of a future may be extracted with the apply() method, which
    // will block if the result is not ready.
    // Since we do want to block until all results are ready, we can call apply()
    // directly instead of using a method such as Futures.awaitAll()
    val results = tasks map (future => future.apply())
    

    There you go. Just that.

提交回复
热议问题