Home > OS >  How to update a sequence using Scala map method?
How to update a sequence using Scala map method?

Time:10-03

I have the following sequence where I am updating every element of the pair by adding 1 by using a for-loop: ​

points: Seq[Point] = Seq((ax,ay), (bx,by), (cx,cy))
for (point <- points)
{
  ​val update = Point(point.x   1, point.y   1)
}

However, I want to use the map method to update my elements in the following way:

val update = points.map(x => (x._1   1, x._2   1))

But it's showing me the following error:

enter image description here

enter image description here

So how can I then update my sequence using Scala map method and get rid of this error?

CodePudding user response:

I think you should write:

val update: Seq[Point] = points.map(point => Point(point.x   1, point.y   1))

As you done in the foor loop.

  • Related