Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Best way to iterate over collection and populate Array

scala noob here, i have a collection (Seq) of xml nodes, and i would like to populate an Array based on each node:

val nodes = data.child \\"package"
var packages = new Array[Package](nodes.length)
var index = 0
for(val entry <- nodes) {
   packages(index) = new Package(entry)
   index = index+1
}

Although it works, does not look much "scala-ish" to me, and i'm sure there's a better way to do it..
Any ideas?

like image 337
marcosbeirigo Avatar asked Dec 02 '10 16:12

marcosbeirigo


1 Answers

(data.child \\ "package") map(new Package(_)) toArray
like image 190
Vasil Remeniuk Avatar answered Sep 20 '22 12:09

Vasil Remeniuk