我在斯卡拉以下功能:有在Future.sequence訪問對象包含未來
object TestFutures5 extends App {
def future (i:Int) = Future { i * 10 }
var rnd = scala.util.Random
val futureResult = (1 to 10).map {
x =>
val y = rnd.nextInt(x)
(future(x),y) // <- this should return a future
// as it needs to be processed by Future.sequence
}
Future.sequence(futureResult).foreach(list => println(list)) // <- this does not compile
Thread.sleep(5000)
}
在Future.sequence
功能,我需要訪問的future(x)
結果和每個變量y
,但由於sequence
僅適用於期貨,此代碼不能編譯。如何重構/修復它?