In continuation of Scala learning curve
I have two lists of objects. I need to merge these lists into one list, while applying a logic with matching pares.
So, for example, here are the two lists:
case class test(int: Int, str: String)
val obj1 = test(1, "one")
val obj2 = test(2, "two")
val list1 = List(obj1, obj2)
val obj3 = test(2, "Another two")
val obj4 = test(4, "four")
val list2 = List(obj1, obj2)
What I need is:
List(test(1, "one old"), test(2, "Another two updated"), test(4, "four new"))
Of coarse, I can iterate though all elements in an old fashioned way, and do all the conversions there, but that is not the "Scala way" (I guess).
I tried approaching it with foldLeft
, but got stuck. Here is what I have that is not working:
list1.foldLeft(list2) { (a:test, b:test) =>
b.int match {
case a.int => {
//Apply logic and create new object
}
}
}
UPDATE For now I did it in two steps:
var tasks : Seq[ChecklistSchema.Task] = left.tasks.map((task:ChecklistSchema.Task) =>
right.tasks.find(t => t.groupId == task.groupId) match {
case Some(t: ChecklistSchema.Task) => t
case _ => {
task.status match {
case TaskAndValueStatus.Active => task.copy(status = TaskAndValueStatus.Hidden)
case _ => task
}
}
}
)
tasks = tasks ++ right.tasks.filter((t:ChecklistSchema.Task) => !tasks.contains(t))
There is got to be a better approach!
Thanks,