How do I make a simple list in scala?
I know that zip can make two lists into one list,
scala> List(1, 2, 3).zip(List("4", "5", "6"))
res0: List[(Int, Int)] = List((1,4), (2,5), (3,6))
however, I don't need 'res0'. I want it,
res0: List[Int] = List(1,4,2,5,3,6)
maybe i should use some "indexOF" or "zipWithIndex" but that seems more verbose than a good "way"
Is there an elegant way to make the list look like this example?
or do I need to use some "IF"?
+3
Gamoonbi
source
to share
1 answer
I think you want to alternate between two lists, also assuming both are the same type. If so, then this should work for you.
List(1,2,3) zip List(4,5,6) flatMap {case (x,y) => List(x,y)}
And the output will be
res0: List[Int] = List(1, 4, 2, 5, 3, 6)
+6
Biswanath
source
to share