nest
nest

Reputation: 347

Scala sum of the arrays contained in an array

I have defined a function that receives an array of arrays. I want to get the sum of all arrays. My question is how to make that sum.

def suma[T](args: WrappedArray[T]*)(implicit n: Numeric[T]) = {
    args.transpose.map(_.sum)
} 
def sum[T](arr: WrappedArray[WrappedArray[T]])(implicit n: Numeric[T]) = {
    val result = suma( ______ )
}

I thought I use the defined "sum" , but not how to pass the contents of the container array. Like there is a much simpler way to do this. Any ideas?

Upvotes: 0

Views: 165

Answers (1)

Dima
Dima

Reputation: 40508

To get "sum of all arrays" you want .flatten, not .transpose. args.flatten.sum should do it.

Or are you asking how to call a function with vargargs? For that, you need a splat operator: val result = suma(arr:_*)

Upvotes: 2

Related Questions