gpt4 book ai didi

kotlin - Kotlin 是否支持一元理解?

转载 作者:IT老高 更新时间:2023-10-28 13:32:31 29 4
gpt4 key购买 nike

像 C# 中的 LINQ,Scala 中的理解,Kotlin 中的类似内容?如果不支持,是否有计划?

最佳答案

没有特殊的关键字(do/for),因此没有像其他语言那样直接翻译成嵌套的 flatMap(去糖)。

但是单子(monad)理解可以通过协程来实现。

来自 Arrow 文档:https://arrow-kt.io/docs/patterns/monad_comprehensions/#comprehensions-over-coroutines

Comprehensions over coroutines

This feature is known with multiple names: async/await, coroutines, do notation, for comprehensions…each version contains certain unique points but all derive from the same principles. In Kotlin, coroutines (introduced in version 1.1 of the language) make the compiler capable of rewriting seemingly synchronous code into asynchronous sequences. Arrow uses this capability of the compiler to bring you coroutines-like notation to all instances of the Monad typeclass.

This means that comprehensions are available for Option, Try, List, Reader, Observable, Flux or IO all the same.

对于协程,另请参阅“Deep into Coroutines on JVM @ KotlinConf 2017”:https://www.slideshare.net/elizarov/deep-dive-into-coroutines-on-jvm-kotlinconf-2017?next_slideshow=1

关于kotlin - Kotlin 是否支持一元理解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34248483/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com