Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element-wise sum of arrays in Scala

Tags:

arrays

scala

How do I compute element-wise sum of the Arrays?

val a = new Array[Int](5)
val b = new Array[Int](5)
// assign values
// desired output: Array -> [a(0)+b(0), a(1)+b(1), a(2)+b(2), a(3)+b(3), a(4)+b(4)]

a.zip(b).flatMap(_._1+_._2)

missing parameter type for expanded function

like image 573
Bob Avatar asked Jan 28 '16 15:01

Bob


People also ask

What is an array in Scala?

Scala | Arrays. Array is a special kind of collection in scala. it is a fixed size data structure that stores elements of the same data type. The index of the first element of an array is zero and the last element is the total number of elements minus one.

How to sum all the elements of a Scala list?

Scala List sum () method with example. Last Updated : 26 Jul, 2019. The sum () method is utilized to add all the elements of the stated list. Method Definition: def sum (num: math.Numeric [B]): B. Return Type: It returns the sum of all the elements of the list.

How to sum the sum of two arrays into a new array?

Digits of element wise sum of two arrays into a new array. Given two arrays of positive integers A and B of sizes M and N respectively, the task is to push A[i] + B[i] into a new array for every i = 0 to min(M, N) and print the newly generated array in the end. If the sum is a two-digit number then break the digits into two elements i.e.

How to concatenate two arrays in Scala?

Here, arr1 is an array of four elements and arr2 is another array of four elements now we concatenate these two array in arr3 by using concat () method. The Multidimensional arrays contains more than one row to store the values. Scala has a method Array.ofDim to create Multidimensional arrays in Scala .


2 Answers

Try:

a.zip(b).map { case (x, y) => x + y }
like image 179
Jean Logeart Avatar answered Oct 03 '22 15:10

Jean Logeart


When you use an underscore as a placeholder in a function definition, it can only appear once (for each function argument position, that is, but in this case flatMap takes a Function1, so there's only one). If you need to refer to an argument more than once, you can't use the placeholder syntax—you'll need to give the argument a name.

As the other answers point out, you can use .map { case (x, y) => x + y } or the tuple accessor version, but it's also worth noting that if you want to avoid a bunch of tuple allocations in an intermediate collection, you can write the following:

scala> (a, b).zipped.map(_ + _)
res5: Array[Int] = Array(0, 0, 0, 0, 0)

Here zipped is a method that's available on pairs of collections that has a special map that takes a Function2, which means the only tuple that gets created is the (a, b) pair. The extra efficiency probably doesn't matter much in most cases, but the fact that you can pass a Function2 instead of a function from pairs means the syntax is often a little nicer as well.

like image 37
Travis Brown Avatar answered Oct 03 '22 16:10

Travis Brown