【发布时间】:2020-08-16 04:40:17
【问题描述】:
我是 Kotlin 的 Arrow 框架的新手,我有几个问题:
假设
fun getUser(id: Int): IO<Option<User>>
fun getCards(user: User): IO<List<Card>>
fun getUserAndCards(id: Int): IO<Option<Pair<User, List<Card>>>> = IO.fx {
when (val user = !userRepository.get(id)) {
is None -> None
is Some -> {
val cards = !cardRepository.get(user.t.id)
Some(Pair(user.t, cards))
}
}
}
如何以“箭头时尚”的方式实现相同的功能?
我设法得到:
fun getUserAndCards(id: Int): IO<Option<Pair<User, List<Card>>>> = IO.fx {
userRepository.get(id).bind().map { user ->
val cards = cardRepository.get(user.id).bind()
Pair(user, cards)
}
}
但我在第二个bind() 中获得Suspension functions can be called only within coroutine body。
编辑:
我看到this post 有同样的问题。在提供的答案中,它说 问题是未涵盖 left/none 选项。 但它已涵盖,当将 map 应用于 None 时,预计会获得 @ 987654328@.
【问题讨论】:
标签: kotlin concurrency functional-programming arrow-kt