How to flatten a nested tuple?

做~自己de王妃 提交于 2019-12-18 11:50:41

问题


I have a nested tuple structure like (String,(String,Double)) and I want to transform it to (String,String,Double). I have various kinds of nested tuple, and I don't want to transform each manually. Is there any convenient way to do that?


回答1:


If you use shapeless, this is exactly what you need, I think.




回答2:


There is no flatten on a Tupple. But if you know the structure, you can do something like this:

implicit def flatten1[A, B, C](t: ((A, B), C)): (A, B, C) = (t._1._1, t._1._2, t._2)
implicit def flatten2[A, B, C](t: (A, (B, C))): (A, B, C) = (t._1, t._2._1, t._2._2)

This will flatten Tupple with any types. You can also add the implicit keyword to the definition. This works only for three elements. You can flatten Tupple like:

(1, ("hello", 42.0))   => (1, "hello", 42.0)
(("test", 3.7f), "hi") => ("test", 3.7f, "hi")

Multiple nested Tupple cannot be flatten to the ground, because there are only three elements in the return type:

((1, (2, 3)),4)        => (1, (2, 3), 4)



回答3:


Not sure about the effiency of this, but you can convert Tuple to List with tuple.productIterator.toList, then flatten the nested lists:

scala> val tuple = ("top", ("nested", 42.0))
tuple: (String, (String, Double)) = (top,(nested,42.0))

scala> tuple.productIterator.map({
     |   case (item: Product) => item.productIterator.toList
     |   case (item: Any) => List(item)
     | }).toList.flatten
res0: List[Any] = List(top, nested, 42.0)


来源:https://stackoverflow.com/questions/13699070/how-to-flatten-a-nested-tuple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!