In response to βwhat if I have more than 3 lists?β:
def combineAll[T](xss: Seq[T]*)(f: (T, T) => T) = xss reduceLeft ((_,_).zipped map f)
Use this:
scala> combineAll(List(1,2,3), List(2,2,2), List(4,5,6), List(10,10,10))(_+_) res5: Seq[Int] = List(17, 19, 21)
edit: alternative
def combineAll[T](xss: Seq[T]*)(f: (T, T) => T) = xss.transpose map (_ reduceLeft f)
does the same and is probably more efficient.
source share