How would we iterate over two consecutive elements of a list and apply the difference function For instance I have this :
val list = List(List("Eat", "Drink", "Sleep", "work"), List("Eat", "Sleep", "Dance"))
I want to iterate over these two consecutive elements and calculate the difference
I've tried this but I do not know how to iterate over each two consecutive elements
list.map((a,b) => a.diff(b))
the output should be List("Drink", "work")
If I understand correctly you probably want to iterate over a sliding window.
list.sliding(2).map{
case List(a, b) => a.diff(b)
case List(a) => a
}.toList
Alternatively you might also want grouped(2)
which partitions the list into groups instead.