Discussion:
ListBuffer[ListBuffer[String]] to List[List[String]]
hangle
2009-09-18 12:35:32 UTC
Permalink
I have employed 'toList' to convert ListBuffer[String] to List[String].
I am, however, at a lost as to how to convert :
ListBuffer[ListBuffer[String]] to List[List[String]]
--
View this message in context: http://www.nabble.com/ListBuffer-ListBuffer-String---to-List-List-String---tp25507927p25507927.html
Sent from the Scala - User mailing list archive at Nabble.com.
Tony Morris
2009-09-18 12:37:39 UTC
Permalink
Sounds hairy. You want the map method.
Post by hangle
I have employed 'toList' to convert ListBuffer[String] to List[String].
ListBuffer[ListBuffer[String]] to List[List[String]]
--
Tony Morris
http://tmorris.net/
Ricky Clarkson
2009-09-18 12:39:12 UTC
Permalink
I'd guess you probably could write the code differently so that you
didn't need this, but:

scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer

scala> val lists: ListBuffer[ListBuffer[Int]] = new ListBuffer
lists: scala.collection.mutable.ListBuffer[scala.collection.mutable.ListBuffer[Int]]
= ListBuffer()

scala> lists += new ListBuffer
res0: lists.type = ListBuffer(ListBuffer())

scala> lists(0) += 5
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(5)

scala> lists map (_.toList) toList
res2: List[List[Int]] = List(List(5))
I have employed  'toList' to convert ListBuffer[String] to List[String].
ListBuffer[ListBuffer[String]] to List[List[String]]
--
View this message in context: http://www.nabble.com/ListBuffer-ListBuffer-String---to-List-List-String---tp25507927p25507927.html
Sent from the Scala - User mailing list archive at Nabble.com.
--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson-***@public.gmane.org
hangle
2009-09-18 15:16:59 UTC
Permalink
Post by Ricky Clarkson
I'd guess you probably could write the code differently so that you
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
scala> val lists: ListBuffer[ListBuffer[Int]] = new ListBuffer
scala.collection.mutable.ListBuffer[scala.collection.mutable.ListBuffer[Int]]
= ListBuffer()
scala> lists += new ListBuffer
res0: lists.type = ListBuffer(ListBuffer())
scala> lists(0) += 5
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(5)
scala> lists map (_.toList) toList
res2: List[List[Int]] = List(List(5))
I have employed  'toList' to convert ListBuffer[String] to List[String].
ListBuffer[ListBuffer[String]] to List[List[String]]
--
http://www.nabble.com/ListBuffer-ListBuffer-String---to-List-List-String---tp25507927p25507927.html
Sent from the Scala - User mailing list archive at Nabble.com.
--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
--
View this message in context: http://www.nabble.com/ListBuffer-ListBuffer-String---to-List-List-String---tp25507927p25510537.html
Sent from the Scala - User mailing list archive at Nabble.com.
hangle
2009-09-18 15:26:40 UTC
Permalink
Post by Ricky Clarkson
I'd guess you probably could write the code differently so that you
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
scala> val lists: ListBuffer[ListBuffer[Int]] = new ListBuffer
scala.collection.mutable.ListBuffer[scala.collection.mutable.ListBuffer[Int]]
= ListBuffer()
scala> lists += new ListBuffer
res0: lists.type = ListBuffer(ListBuffer())
scala> lists(0) += 5
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(5)
scala> lists map (_.toList) toList
res2: List[List[Int]] = List(List(5))
I have employed  'toList' to convert ListBuffer[String] to List[String].
ListBuffer[ListBuffer[String]] to List[List[String]]
--
http://www.nabble.com/ListBuffer-ListBuffer-String---to-List-List-String---tp25507927p25507927.html
Sent from the Scala - User mailing list archive at Nabble.com.
--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
i had tried ' lists foreach(_ toList)toList' . when i saw the use of 'map'
i realized that i was NOT returning a List such as List[List[String]]
--
View this message in context: http://www.nabble.com/ListBuffer-ListBuffer-String---to-List-List-String---tp25507927p25510697.html
Sent from the Scala - User mailing list archive at Nabble.com.
Ricky Clarkson
2009-09-18 15:31:27 UTC
Permalink
foreach does stuff, rather than returns stuff. You can tell this
because its return type is Unit.
Post by Ricky Clarkson
I'd guess you probably could write the code differently so that you
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
scala> val lists: ListBuffer[ListBuffer[Int]] = new ListBuffer
scala.collection.mutable.ListBuffer[scala.collection.mutable.ListBuffer[Int]]
= ListBuffer()
scala> lists += new ListBuffer
res0: lists.type = ListBuffer(ListBuffer())
scala> lists(0) += 5
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(5)
scala> lists map (_.toList) toList
res2: List[List[Int]] = List(List(5))
I have employed  'toList' to convert ListBuffer[String] to List[String].
ListBuffer[ListBuffer[String]] to List[List[String]]
--
http://www.nabble.com/ListBuffer-ListBuffer-String---to-List-List-String---tp25507927p25507927.html
Sent from the Scala - User mailing list archive at Nabble.com.
--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
i had tried ' lists  foreach(_ toList)toList' .  when i saw the use of 'map'
i realized that i was NOT returning a List such as  List[List[String]]
--
View this message in context: http://www.nabble.com/ListBuffer-ListBuffer-String---to-List-List-String---tp25507927p25510697.html
Sent from the Scala - User mailing list archive at Nabble.com.
--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson-***@public.gmane.org
Loading...