How can I get a sum of arrays of tuples in scala
scala
Solution
There is no such thing as `Tuple` addition, so that can't work. You would have to operate on each ordinate of the `Tuple`:
val res = arr.foldLeft(0,0){ case (sum, next) => (sum._1 + next._1, sum._2 + next._2) }
res: (Int, Int) = (25,30)
Problem
I have a simple array of tuples ``` val arr = Array((1,2), (3,4),(5,6),(7,8),(9,10)) ``` I wish to get `(1+3+5+7+9, 2+4+6+8+10)` tuple as the answer What is the best way to get the sum as tuples, similar to regular arrays. I tried ``` val res = arr.foldLeft(0,0)(_ + _) ``` This does not work. Sorry about not writing the context. I was using it in scalding with algebird. Algebird allows sums of tuples and I assumed this would work. That was my mistake.