我有一个嵌套的元组结构(String,(String,Double))
,我想将它转换为(String,String,Double)
. 我有各种嵌套元组,我不想手动转换每个。有什么方便的方法吗?
问问题
14587 次
5 回答
9
元组上没有展平。但是如果你知道结构,你可以这样做:
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)
这将使任何类型的 Tuple 变平。您还可以将隐式关键字添加到定义中。这仅适用于三个元素。你可以像这样展平元组:
(1, ("hello", 42.0)) => (1, "hello", 42.0)
(("test", 3.7f), "hi") => ("test", 3.7f, "hi")
多个嵌套的 Tuple 不能展平到地面,因为返回类型中只有三个元素:
((1, (2, 3)),4) => (1, (2, 3), 4)
于 2012-12-04T09:17:27.040 回答
1
不确定这样做的效率,但您可以转换Tuple
为List
with tuple.productIterator.toList
,然后flatten
是嵌套列表:
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)
于 2018-04-19T09:31:24.943 回答
1
以上答案的补充
粘贴此实用程序代码:
import shapeless._
import ops.tuple.FlatMapper
import syntax.std.tuple._
trait LowPriorityFlatten extends Poly1 {
implicit def default[T] = at[T](Tuple1(_))
}
object flatten extends LowPriorityFlatten {
implicit def caseTuple[P <: Product](implicit lfm: Lazy[FlatMapper[P, flatten.type]]) =
at[P](lfm.value(_))
}
那么你就可以展平任何嵌套的元组:
scala> val a = flatten(((1,2),((3,4),(5,(6,(7,8))))))
a: (Int, Int, Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6,7,8)
请注意,此解决方案不适用于自定义
case class
类型,它将String
在输出中转换为。scala> val b = flatten(((Cat("c"), Dog("d")), Cat("c"))) b: (String, String, String) = (c,d,c)
于 2021-03-08T22:53:45.843 回答
0
在我看来,简单的模式匹配会起作用
scala> val motto = (("dog", "food"), "tastes good")
val motto: ((String, String), String) = ((dog,food),tastes good)
scala> motto match {
| case ((it, really), does) => (it, really, does)
| }
val res0: (String, String, String) = (dog,food,tastes good)
或者,如果您有一组这样的元组:
scala> val motto = List(
| (("dog", "food"), "tastes good")) :+ (("cat", "food"), "tastes bad")
val motto: List[((String, String), String)] = List(((dog,food),tastes good), ((cat,food),tastes bad))
scala> motto.map {
| case ((one, two), three) => (one, two, three)
| }
val res2: List[(String, String, String)] = List((dog,food,tastes good), (cat,food,tastes bad))
我认为即使您有几个案例也会很方便。
于 2021-03-02T13:57:59.817 回答