How to sum a list of tuples

前端 未结 5 1367
囚心锁ツ
囚心锁ツ 2021-01-02 04:06

Given the following list of tuples...

val list = List((1, 2), (1, 2), (1, 2))

... how do I sum all the values and obtain a single tuple lik

5条回答
  •  -上瘾入骨i
    2021-01-02 04:42

    Scalaz solution (suggestied by Travis and for some reason a deleted answer):

    import scalaz._
    import Scalaz._
    
    val list = List((1, 2), (1, 2), (1, 2))
    list.suml
    

    which outputs

    res0: (Int, Int) = (3,6)
    

提交回复
热议问题