Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala kotlin中是否有Future.sequence模拟?_Scala_Kotlin_Future_Completable Future - Fatal编程技术网

Scala kotlin中是否有Future.sequence模拟?

Scala kotlin中是否有Future.sequence模拟?,scala,kotlin,future,completable-future,Scala,Kotlin,Future,Completable Future,在scala中,您可以将未来集合“映射”到集合的未来,如下所示: val l: List[Future[String]] = List(Future {"1"}, Future {"2"}) val x: Future[List[String]] = Future.sequence(l) 如何使用kotlin实现相同的功能?假设您使用协同程序: val l: List<Deferred<String>> = (1..2).map {i -> async(Unc

在scala中,您可以将未来集合“映射”到集合的未来,如下所示:

val l: List[Future[String]]  = List(Future {"1"}, Future {"2"})
val x: Future[List[String]] = Future.sequence(l)

如何使用kotlin实现相同的功能?

假设您使用协同程序:

val l: List<Deferred<String>>  = (1..2).map {i -> async(Unconfined){ "$i" }}
val x: Deffered<List<String>> = async(Unconfined) { l.map {it.await()} }
vall:List=(1..2).map{i->async(未定义){“$i”}
val x:Deffered=async(未定义){l.map{it.await()}

假设您使用协同程序:

val l: List<Deferred<String>>  = (1..2).map {i -> async(Unconfined){ "$i" }}
val x: Deffered<List<String>> = async(Unconfined) { l.map {it.await()} }
vall:List=(1..2).map{i->async(未定义){“$i”}
val x:Deffered=async(未定义){l.map{it.await()}

如果由于Java互操作而必须使用
CompletebleFuture
,则可以使用几乎相同的代码将
List
转换为
CompletebleFuture
,只需将
async
builder替换为
future
builder from
kotlinx-coroutines-jdk8
如果由于Java互操作而必须使用
CompletableFuture
,则可以使用几乎相同的代码将
List
转换为
CompletableFuture
,只需将
async
builder替换为
kotlinx-coroutines-jdk8
中的
future
builder,向下投票时请发表评论。向下投票时请发表评论。