How to make a Vector call

Hi,

I tried to run the following code and tried to call the function but somehow it didn’t work. Do you know the reason?
def scalarProduct(xs: Vector[Double], ys: Vector[Double]): Double =

xs.zip(ys).map(_ * _).sum

scalarProduct(Vector(1.0,2.0,3.0), Vector(1.0,2.0,3.0))

Thanks

What do you mean it doesn’t work?
Doesn’t it compile? If so, which error do you get?
Doesn’t it run? If so, which exception do you get?
Doesn’t it produce the correct output? If so what was the result and what did you expect?

For what I can see that code is correct, maybe you have a problem with your environment setup?
If so, maybe a more interactive channel like the gitter chat would be better?

Hi Fantoh.

When you do xs zip ys you get a Vector((Double, Double)). In order to perform map with tuples you need to use case (x,y) in your function. For example,

xs.zip(ys).map{case (x,y) => x * y}.sum

should work. In Scala 3, that necessity has been removed, so you can do it like the way you did it.

1 Like

Hi, I have created a worksheet in visual studio to see my output immediately. When I put this code I see the the following error message for both underscore _ _

missing parameter type for expanded function.

Thanks a lot dhinojosa! Now it works for me.