BobCoder
BobCoder

Reputation: 823

Reorder of Tuple members in scala

How can I reorder members of a tuple ? I have a list of tuples of 2 as below

((115,vp,London,1001),(2,ZIP1,ZIP2))

I want to reorder the tuple as

 ((vp,London), ( 115,1001,2,ZIP1,ZIP2) )

Upvotes: 0

Views: 71

Answers (1)

joesan
joesan

Reputation: 15435

So based on your assumption that the elements of the tuple appear in the same position, you can do the following:

val tpl = Seq(((115,"vp","London",1001),(2,"ZIP1","ZIP2")))

tpl.map {
  case (elem1, elem2) => ((elem1._2, elem1._3), (elem1._1, elem1._4, elem2._1, elem2._2, elem2._3))
}

A much better way would be to use case classes instead of tuples like this! You can box the elements of your tuple into a case class and then map is as you want it!

Upvotes: 2

Related Questions