Kotlin是否支持单语理解? [英] Does Kotlin support monadic comprehension?

查看:75
本文介绍了Kotlin是否支持单语理解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像C#中的LINQ,对于Scala中的理解,在Kotlin中有什么类似的东西吗?如果不支持,计划了吗?

Like LINQ in C#, for comprehension in Scala, anything similar in Kotlin? If not supported, is it planned?

推荐答案

没有特殊的关键字(do/for),因此无法像其他语言一样直接翻译为嵌套的flatMap(删除).

There is no special keyword (do/for) and so no direct translation to nested flatMap (desugaring) like in other languages.

但是,可以通过协程实现对单子的理解.

But monadic comprehension can be implemented with coroutines.

摘自Arrow文档: https://arrow-kt. io/docs/patterns/monad_comprehensions/#comprehensions-over-协程

From Arrow documentation : https://arrow-kt.io/docs/patterns/monad_comprehensions/#comprehensions-over-coroutines

对协程的理解

Comprehensions over coroutines

此功能有多个名称:异步/等待,协程,执行 表示法,用于理解...每个版本都包含某些唯一 点,但都是基于相同的原理.在科特林,协程 (在该语言的版本1.1中引入)使编译器能够 将看似同步的代码重写为异步序列的过程. Arrow使用编译器的此功能为您带来 Monad类型类的所有实例的类似协程的表示法.

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.

这意味着可以对Option,Try,List, 读取器,Observable,Flux或IO都相同.

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

对于协程,另请参阅深入探究JVM @ KotlinConf 2017上的协程":

For coroutines, see also "Deep dive into Coroutines on JVM @ KotlinConf 2017" : https://www.slideshare.net/elizarov/deep-dive-into-coroutines-on-jvm-kotlinconf-2017?next_slideshow=1

这篇关于Kotlin是否支持单语理解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆