Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I yield or map one element into many in Scala?

Tags:

scala

val inArray = Array("a", "b", "c", "d")
// ...
val outArray = Array("a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3", "d1", "d2", "d3")

How to map inArray to outArray?

The idea is to iterate through inArray yielding 3 elements (by concatenating an index in this example) from each of its elements.

like image 865
Ivan Avatar asked Oct 28 '12 15:10

Ivan


Video Answer


1 Answers

You can do that with flatMap.

inArray.flatMap(c => (1 to 3).map(c+))
like image 142
Kim Stebel Avatar answered Sep 25 '22 14:09

Kim Stebel