What is the Scala equivalent of F#'s async workflows?

前端 未结 1 394
情话喂你
情话喂你 2021-01-01 20:11

What is the Scala equivalent of F#\'s async workflows?

For example, how would following F# snippet translate to idiomatic Scala?

open System.Net
open         


        
1条回答
  •  -上瘾入骨i
    2021-01-01 20:41

    You code more or less directly can be translated to Scala using Futures (with some important features lost, though):

    import scala.actors.Futures
    import Futures._
    
    val urlList = Map("Microsoft.com" -> "http://www.microsoft.com/",
                    "MSDN" -> "http://msdn.microsoft.com/",
                    "Bing" -> "http://www.bing.com")
    
    
    def fetchAsync(name: String, url: String) = future {
        // lengthy operation simulation
        Thread.sleep(1000)
        println("Fetching from %s: %s" format(name, url))
    }
    
    def runAll = 
        //Futures.awaitAll(  <- if you want to synchronously wait for the futures to complete 
        urlList.map{case (name, url) => fetchAsync(name, url)}
        //)
    

    0 讨论(0)
提交回复
热议问题