0

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?

nest
  • 347
  • 2
  • 5
  • 17

1 Answers1

2

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:_*)

Dima
  • 39,570
  • 6
  • 44
  • 70