Kotlin 将 List<Triple<String, String, String> 变异为 Triple<List<String>, List<String>, List<String>> 的优雅方式

标签 kotlin functional-programming monads monad-transformers arrow-kt

我想尽可能简洁地(但清楚地)转换 List<Triple<String, String, String>Triple<List<String>, List<String>, List<String>> .
例如,假设执行转换的方法称为 turnOver ,我希望:

val matches = listOf(
  Triple("a", "1", "foo"),
  Triple("b", "2", "bar"),
  Triple("c", "3", "baz"),
  Triple("d", "4", "qux")
)
val expected = Triple(
  listOf("a", "b", "c", "d"),
  listOf("1", "2", "3", "4"),
  listOf("foo", "bar", "baz", "qux")
)
matches.turnOver() == expected // true
如何编写简洁、清晰且可能具有功能性的文章 turnOver功能?
可以使用 Arrow-Kt,我已经将其作为项目依赖项。

最佳答案

fun turnOver(matches: List<Triple<String, String, String>>) = Triple(
   matches.map { it.first },
   matches.map { it.second },
   matches.map { it.third },
)
我认为将是一个明显的解决方案。

关于Kotlin 将 List<Triple<String, String, String> 变异为 Triple<List<String>, List<String>, List<String>> 的优雅方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62675056/

相关文章:

android - 在与 Kotlin 兼容的新 Android Q Preview SDK 中

kotlin - 使用Room Persistence库,我可以设置自己的主键并自己管理它的 “uniqueness”吗?

scala - 如何将函数的返回类型指定为(任意)monad?

android - 如何在Kotlin中以编程方式设置闪光布局?

kotlin - 在比较中忽略三元组的组成部分

functional-programming - Foldl 在 SML 中返回一个元组?

functional-programming - 实例化对象的方法语法 : can't seem to get it right

database - 函数式编程和数据库交互的最佳实践是什么?

haskell - 如何使用 Monad 的 (->) 实例以及 (->) 的困惑

haskell - 为什么 ParsecT 没有 MonadWriter 实例?