How to flatten a nested tuple?

前端 未结 3 931
野趣味
野趣味 2020-12-29 05:55

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

3条回答
  •  滥情空心
    2020-12-29 06:29

    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)
    

提交回复
热议问题