gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:32:30 27 4
gpt4 key购买 nike

我想尽可能简洁地(但清楚地)转换 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com