[英]Scala tuple add keeping Sequence same order
I want to add scala tuples in respective order, it should add to list in its sequence我想按各自的顺序添加 scala 元组,它应该按顺序添加到列表中
val d = (List.empty[String],List.empty[String],List.empty[String])
("1","2","3") :: d
("4","5","6") :: d
d should give output like (List("1","4"), List("2","5"),List("3","6"))
d 应该给出这样的输出
(List("1","4"), List("2","5"),List("3","6"))
I have header data of below type and from the entity, I want to extract data of few attributes and accumulate as a tuple, I tried below approach which extracts data from the entity but not matching missing data type我有以下类型和实体的标题数据,我想提取少数属性的数据并作为元组累积,我尝试了以下从实体中提取数据但不匹配缺失数据类型的方法
header type Task[Map[String,List[Entity]]]
标头类型
Task[Map[String,List[Entity]]]
val missing:(Set[String],Set[String],Set[String]) = headers.map(_.foldLeft((Set.empty[String],Set.empty[String],Set.empty[String))((a,v)=> {
v._2.map {
entity =>{
val x = entity.field1++a._1
val y = entity.field2++a._2
val z = entity.field3++a._3
(x,y,z)
}
}
}
))
Above data type does not match and get compilation error.以上数据类型不匹配并出现编译错误。
Something like this?像这样的东西?
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))
You could use shapeless, which gives you the possibility to create generic functions working over tuples with various arity.您可以使用 shapeless,这使您可以创建在具有各种元组的元组上工作的通用函数。 First, you need to create functions:
首先,您需要创建函数:
import shapeless.syntax.std.tuple._
import cats.implicits._
object lifter extends (Id ~> List) { //shapeless polymorphic function lifting value into List
override def apply[T](f: Id[T]): List[T] = List(f)
}
object folder extends Poly1 { //function which folds two lists using monoid
implicit def caseTupleOfLists[A](implicit monoid: Monoid[A]) = at[(List[A], List[A])]{case (a, b) => a |+| b}
}
Then you can use it like this:然后你可以像这样使用它:
val a = ("1", "2", "3").map(lifter) //(List(1),List(2),List(3))
val b = ("4", "5", "6").map(lifter) //(List(4),List(5),List(6))
val result = a.zip(b).map(folder) //(List(1, 4),List(2, 5),List(3, 6))
One benefit over jwvh's solutions is, that it will work also on tuples with different arity: jwvh 解决方案的一个好处是,它也适用于具有不同数量的元组:
val a2 = ("1", "2", "3", "foo").map(lifter)
val b2 = ("4", "5", "6", "bar").map(lifter)
val result2 = a2.zip(b2).map(folder) //(List(1, 4),List(2, 5),List(3, 6),List(foo, bar))
If you map over Task
(whether it's from ZIO, Monix or another library) you can't just get a result like (Set[String],Set[String],Set[String])
.如果您映射
Task
(无论它是来自 ZIO、Monix 还是其他库),您不能只得到类似(Set[String],Set[String],Set[String])
。 You can only get Task[(Set[String], Set[String], Set[String])]
.你只能得到
Task[(Set[String], Set[String], Set[String])]
。 I modified your code to use pattern matching (it's usually more readable this way) and now it compiles:我修改了您的代码以使用模式匹配(这种方式通常更具可读性),现在它可以编译:
case class Entity(field1: String, field2: String, field3: String)
val headers: Task[Map[String, List[Entity]]] = ???
val missing: Task[(Set[String], Set[String], Set[String])] =
headers.map(_.foldLeft((Set.empty[String], Set.empty[String], Set.empty[String])) {
case ((fields1, fields2, fields3), _ -> Entity(field1, field2, field3)) => {
val x = fields1 + field1
val y = fields2 + field2
val z = fields3 + field3
(x, y, z)
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.