Search code examples
scalashapeless

Add list of tuples of integers in Scala


I wish to add a list of tuples of integers i.e. given an input list of tuples of arity k, produce a tuple of arity k whose fields are sums of corresponding fields of the tuples in the list.

Input

List( (1,2,3), (2,3,-3), (1,1,1)) 

Output

(4, 6, 1)

I was trying to use foldLeft, but I am not able to get it to compile. Right now, I am using a for loop, but I was looking for a more concise solution.


Solution

  • This can be done type safely and very concisely using shapeless,

    scala> import shapeless._, syntax.std.tuple._
    import shapeless._
    import syntax.std.tuple._
    
    scala> val l = List((1, 2, 3), (2, 3, -1), (1, 1, 1))
    l: List[(Int, Int, Int)] = List((1,2,3), (2,3,-1), (1,1,1))
    
    scala> l.map(_.toList).transpose.map(_.sum)
    res0: List[Int] = List(4, 6, 3)
    

    Notice that unlike solutions which rely on casts, this approach is type safe, and any type errors are detected at compile time rather than at runtime,

    scala> val l = List((1, 2, 3), (2, "foo", -1), (1, 1, 1))
    l: List[(Int, Any, Int)] = List((1,2,3), (2,foo,-1), (1,1,1))
    
    scala> l.map(_.toList).transpose.map(_.sum)
    <console>:15: error: could not find implicit value for parameter num: Numeric[Any]
                  l.map(_.toList).transpose.map(_.sum)
                                              ^