I have a case class:
case class Nums(num1: Int, num2: Int)
and a list as follows:
val listOfNums = List(Nums(1,1), Nums(2,2))
I want to return Nums(3,3)
which is the sum of all the relevant fields with each other. The way I achieved this was:
listOfNums.foldLeft(Nums(0,0))((acc,nums) => {
acc.copy(
num1 = acc.num1 + nums.num1,
num2 = acc.num2 + nums.num2
)
})
But this feels a bit clumsy, what would be the right way?