Which is the fastest idiomatic way to add all vectors (in the math sense) inside a Scala list?
- by davips
I have two solutions, but one doesn't compile and the other, I think, could be better:
object Foo extends App {
val vectors = List(List(1,2,3), List(2,2,3), List(1,2,2)) //just a stupid example
//transposing
println("vectors = " + vectors.transpose.map (_.sum)) //it prints vectors = List(4, 6, 8)
//folding
vectors.reduce {
case (a, b) => (a zip b) map {
case (x, y) => x + y
}
} //compiler says: missing parameter type for exp. function; arg. types must be fully known
}