scala> val l = List((1,2), (2,3))
l: List[(Int, Int)] = List((1,2), (2,3))
I can do
scala> (0 /: l) {(a, i) => i._1 + a}
Note that, in addition to Walter's answer, you can use the underscore _
instead of c
in the pattern match:
val i = (0 /: l) { case (a, (b, _)) => b + a }
It acts as an anything goes placeholder in the Tuple2
pattern and (to my mind) makes it clearer that you don't actually use the value in your calculation.
Also, in 2.8 you would be able to do:
val i = l.map(_._1).sum
Give this a try:
(0 /: l) { case (a, (b, c)) => b + a }