Reputation: 326
Lets say I have a List:
val list = List(1, 3, 4, 5, 6)
How to sum each element with the next one to create a List of summed values of pairs? In my example I want to get:
List(4, 7, 9, 11)
What operations should I write to do this in a functional fashion?
Upvotes: 0
Views: 165
Reputation: 326
Okay, so after a bit of search I found the sliding(...)
function:
val list = List(1, 3, 4, 5, 6)
list.sliding(2).toSeq.map(_.sum) //List(4, 7, 9, 11)
And this works well
Upvotes: 3