Scala元组添加,保持序列相同顺序

问题描述 投票:0回答:1

我想按各自的顺序添加scala元组,它应该按顺序添加到列表中

val d = (List.empty[String],List.empty[String],List.empty[String])
("1","2","3") :: d 
("4","5","6") :: d 

d应该提供类似(List("1","4"), List("2","5"),List("3","6"))的输出

scala scala-cats
1个回答
0
投票

这样的事情?

implicit class TriplePrepend[A](t :(List[A],List[A],List[A])) {
  def :: (x :(A,A,A)) = (x._1 :: t._1, x._2 :: t._2, x._3 :: t._3)
}

val d = (List.empty[String],List.empty[String],List.empty[String])

("1","2","3") :: ("4","5","6") :: d 
//res0: (List(1, 4),List(2, 5),List(3, 6))
© www.soinside.com 2019 - 2024. All rights reserved.