I am trying to write idiomatic scala code to loop through two lists of lists, and to generate a new list containing only the differences of the two lists.
In procedural Scala I would do something like this:
val first: List[List[Int]] = List(List(1,2,3,4,5),List(1,2,3,4,5), List(1,2,3,4,5))
val second: List[List[Int]] = List(List(1,2,3,4,5),List(1,23,3,45,5),List(1,2,3,4,5))
var diff: List[String] = List[String]()
for (i <- List.range(0, first.size)){
for (j <- List.range(0, first(0).size)){
println(first(i)(j) + " " + second(i)(j))
if (first(i)(j) != second(i)(j)) diff = diff ::: (s"${second(i)(j)}" :: Nil)
}
}
Of course I do not like this, I have attempted to write a solution using for comprehension, but without success.
The closest thing I could get to was this:
for {(lf,ls) <- (first zip second) } yield if (lf == ls) lf else ls
but from that for comprehension I can not generate a list of String being of a different type from the input one.
Any suggestion?