从Mono / Flux到Flow的flatMapMany和flatMap的等效性(Kotlin Coroutines)

时间:2019-11-17 21:29:05

标签: spring-webflux kotlin-coroutines

我正在尝试重构此代码:

fun getCharacterFilms(serverRequest: ServerRequest): Mono<ServerResponse> {
    val films = starWarsApiWebClient.findCharacter(serverRequest.pathVariable("id").toInt())
            .flatMapMany { it.films.toFlux() }
            .flatMap(starWarsApiWebClient::findFilm)

    return ok().body(films, Film::class.java)
}

其中:

fun findCharacter(id: Int): Mono<Character> {...}

fun findFilm(uri: URI): Mono<Film> {...}

但现在使用Flow:

fun findCharacter(id: Int) = webClient.baseUrl("https://swapi.co/api/").build()
            .get().uri("/people/$id/")
            .header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
            .accept(MediaType.APPLICATION_JSON).retrieve()
            .bodyToFlow<Character>()

fun findFilm(uri: URI): Flow<Film> {
    val webClient = WebClient.builder()
    return webClient.build()
            .get().uri(uri)
            .header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
            .accept(MediaType.APPLICATION_JSON).retrieve()
            .bodyToFlow<Film>()
}

没有问题:

suspend fun getCharacter(serverRequest: ServerRequest) = ok().bodyAndAwait(starWarsApiWebClient.findCharacter(serverRequest.pathVariable("id").toInt()))

但是我不知道如何翻译这些flatMapMany和flatMap。

谢谢。

1 个答案:

答案 0 :(得分:0)

我明白了

for (i in 0:20){
    Idents(immune) = "seurat_clusters"
    immune.i = subset(immune, idents = ("i"))
    Idents(immune.i) = "stim"
    immune.i_markers = FindAllMarkers(object=immune.i, only.pos=TRUE, logfc.threshold=0.25)
    write.csv(immune.i_markers,"immune.imarkers.csv")
    save(file='immune.i.Rdata',immune.i)
}