val a= ((1,"one"),2) a-6ren">
gpt4 book ai didi

scala - 如何展平元组,因为 flatMap 不是在元组上定义的?

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

举个例子,如何将((1,"one"),2)转换成(1,"one",2)

我尝试使用 flatMap,但是 flatMap 没有在元组上定义

scala> val a= ((1,"one"),2)
a: ((Int, String), Int) = ((1,one),2)

最佳答案

flatMap 在 Shapeless 2.0 库中定义:

https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0

((23, "foo"), (), (true, 2.0)) flatMap identity

或者你可以将元组转换为迭代器,但在那种情况下类型会丢失

((23, "foo"), (), (true, 2.0)).productIterator

关于scala - 如何展平元组,因为 flatMap 不是在元组上定义的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903823/

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