【发布时间】:2021-04-14 00:50:18
【问题描述】:
我想知道是否可以在具有单子属性的列表或类似列表的结构上实现类似于 Kotlin 中 Haskell 的 do-notation 的东西。
举个例子:
fun <A, B> cartesianProduct(xs: List<A>, ys: List<B>): List<Pair<A, B>> =
xs.flatMap { x -> ys.flatMap { y -> listOf(x to y) } }
如果我能写出类似的东西就好了
suspend fun <A, B> cartesianProduct(xs: List<A>, ys: List<B>): List<Pair<A, B>> =
list {
val x = xs.bind()
val y = xs.bind()
yield(x to y)
}
Arrow-Kt 使用协程为 either, nullable, option and eval 定义了类似的理解。我查看了实现及其Effect documentation,但我无法将概念转换为列表。这在 kotlin 中是否可行?
【问题讨论】:
标签: kotlin kotlin-coroutines arrow-kt