我想知道是否可以在具有单子属性的列表或类似列表的结构上实现类似于 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 使用协程为nullable、option 和 eval定义了类似的理解。我查看了实现及其效果文档,但我无法将概念转换为列表。这在 kotlin 中是否可行?