« Return to Thread: Small Scala application

Re: [scala] Small Scala application

by Bill Burdick-2 :: Rate this Message:

Reply to Author | View in Thread

I believe the reason your code doesn't do what you want is that kannelComment is probably an immutable list stored in a val.  If you make it a mutable list, your code should work, except that you probably want to declare it as List[String]().  Another option might be to use a var instead of a val and reassign kannelComment by using += instead of +.  Here's the idea:

  var kannelComment = List[String]()

  for(line <-list)
      kannelComment += "#" + line


An easier solution might be this:

  val kannelComment = for (line <- list) yield "#" + line


Bill


On Mon, Oct 12, 2009 at 10:12 AM, ammarz <mwaggef.3la.janb@...> wrote:

I wanted to do a small program in Scala that does the following:
Takes a string, and for each line prepend "#" (hash) to the line

I wanted to do as functional as possible but I didn't know how.

I wrote the following and it didnt work:
    val kannelComment = List()
    for(line <-list)
       kannelComment + ("#" line)
where list is a list of Strings.

Could anybody help?
--
View this message in context: http://www.nabble.com/Small-Scala-application-tp25851946p25851946.html
Sent from the Scala mailing list archive at Nabble.com.


 « Return to Thread: Small Scala application