How to convert List to ListBuffer?

23.3k views Asked by At

Is there any way to efficiently do this, perhaps through toBuffer or to methods? My real problem is I'm building a List off a parser, as follows:

lazy val nodes: Parser[List[Node]] = phrase(( nodeA | nodeB | nodeC).*)

But after building it, I want it to be a buffer instead - I'm just not sure how to build a buffer straight from the parser.

1

There are 1 answers

4
Régis Jean-Gilles On BEST ANSWER

to indeed does the trick, and it is pretty trivial to use:

scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
scala> l.to[ListBuffer]
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

Works in scala 2.10.x

For scala 2.9.x, you can do:

scala> ListBuffer.empty ++= l
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)