Small Scala application

View: New views
10 Messages — Rating Filter:   Alert me  

Small Scala application

by ammarz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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?

Re: [scala] Small Scala application

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Firstly, "#" line isn't Scala; you need to use + to concatenate Strings.

Secondly, kannelComment + foo doesn't *do* anything, and its result is
ignored.  A simpler example:

for (x <- 1 to 10)
  3 + 4

The result of 3 + 4 is never used.  What did you want to happen to the
result of kannelComment + foo?

You didn't provide the definition of line, which might be useful in
the next iteration.  Or the compiler error message.

2009/10/12 ammarz <mwaggef.3la.janb@...>:

>
> 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.
>
>



--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...

Re: [scala] Small Scala application

by Andrew Gaydenko :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Monday 12 October 2009 12:12:52 ammarz 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?
>

val kannelComment = list.zipWithIndex.map(t => "" + (t._2 + 1) + " " + t._1)

P.S. Probably it is for scala-user list.

Re: [scala] Small Scala application

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's a very good answer to a different question.

2009/10/12 Andrew Gaydenko <a@...>:

> On Monday 12 October 2009 12:12:52 ammarz 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?
>>
>
> val kannelComment = list.zipWithIndex.map(t => "" + (t._2 + 1) + " " + t._1)
>
> P.S. Probably it is for scala-user list.
>



--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...

Re: [scala] Small Scala application

by ammarz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am sorry for the typos

This is the whole program:

object Main {

  /**
   * @param args the command line arguments
   */
  def main(args: Array[String]) :Unit =
  {
     val list = List("Hi", "Bye", "AMMAR", "AAAA")
     println(list)

     val kannelComment = List()
     for(line <-list)
        kannelComment + ("#" + line)
     //I would like a list of ("#Hi", "#Bye", "#AMMAR", "#AAAA")
  }

}


Ricky Clarkson wrote:
Firstly, "#" line isn't Scala; you need to use + to concatenate Strings.

Secondly, kannelComment + foo doesn't *do* anything, and its result is
ignored.  A simpler example:

for (x <- 1 to 10)
  3 + 4

The result of 3 + 4 is never used.  What did you want to happen to the
result of kannelComment + foo?

You didn't provide the definition of line, which might be useful in
the next iteration.  Or the compiler error message.

2009/10/12 ammarz <mwaggef.3la.janb@gmail.com>:
>
> 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.
>
>



--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@gmail.com

Re: [scala] Small Scala application

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

list map ('#' + _)

2009/10/12 ammarz <mwaggef.3la.janb@...>:

>
> I am sorry for the typos
>
> This is the whole program:
>
> object Main {
>
>  /**
>   * @param args the command line arguments
>   */
>  def main(args: Array[String]) :Unit =
>  {
>     val list = List("Hi", "Bye", "AMMAR", "AAAA")
>     println(list)
>
>     val kannelComment = List()
>     for(line <-list)
>        kannelComment + ("#" + line)
>     //I would like a list of ("#Hi", "#Bye", "#AMMAR", "#AAAA")
>  }
>
> }
>
>
>
> Ricky Clarkson wrote:
>>
>> Firstly, "#" line isn't Scala; you need to use + to concatenate Strings.
>>
>> Secondly, kannelComment + foo doesn't *do* anything, and its result is
>> ignored.  A simpler example:
>>
>> for (x <- 1 to 10)
>>   3 + 4
>>
>> The result of 3 + 4 is never used.  What did you want to happen to the
>> result of kannelComment + foo?
>>
>> You didn't provide the definition of line, which might be useful in
>> the next iteration.  Or the compiler error message.
>>
>> 2009/10/12 ammarz <mwaggef.3la.janb@...>:
>>>
>>> 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.
>>>
>>>
>>
>>
>>
>> --
>> Ricky Clarkson
>> Java Programmer, AD Holdings
>> +44 1565 770804
>> Skype: ricky_clarkson
>> Google Talk: ricky.clarkson@...
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Small-Scala-application-tp25851946p25852130.html
> Sent from the Scala mailing list archive at Nabble.com.
>
>



--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...

Re: [scala] Small Scala application

by Jan Kriesten-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

> object Main {
>
>   /**
>    * @param args the command line arguments
>    */
>   def main(args: Array[String]) :Unit =
>   {
>      val list = List("Hi", "Bye", "AMMAR", "AAAA")
>      println(list)
>
>      val kannelComment = List()
>      for(line <-list)
>         kannelComment + ("#" + line)
>      //I would like a list of ("#Hi", "#Bye", "#AMMAR", "#AAAA")
>   }
> }

instead of the for just have:

val newList = list.map( "#" + _ )

Regards, --- Jan.



Re: [scala] Small Scala application

by Andrew Gaydenko :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Monday 12 October 2009 12:29:33 ammarz wrote:

> I am sorry for the typos
>
> This is the whole program:
>
> object Main {
>
>   /**
>    * @param args the command line arguments
>    */
>   def main(args: Array[String]) :Unit =
>   {
>      val list = List("Hi", "Bye", "AMMAR", "AAAA")
>      println(list)
>
>      val kannelComment = List()
>      for(line <-list)
>         kannelComment + ("#" + line)
>      //I would like a list of ("#Hi", "#Bye", "#AMMAR", "#AAAA")
>   }
>
> }

val kannelComment = list.map(e => "#" + e)


>
> Ricky Clarkson wrote:
> > Firstly, "#" line isn't Scala; you need to use + to concatenate Strings.
> >
> > Secondly, kannelComment + foo doesn't *do* anything, and its result is
> > ignored.  A simpler example:
> >
> > for (x <- 1 to 10)
> >   3 + 4
> >
> > The result of 3 + 4 is never used.  What did you want to happen to the
> > result of kannelComment + foo?
> >
> > You didn't provide the definition of line, which might be useful in
> > the next iteration.  Or the compiler error message.
> >
> > 2009/10/12 ammarz <mwaggef.3la.janb@...>:
> >> 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.
>

Re: [scala] Small Scala application

by Andrew Gaydenko :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Monday 12 October 2009 12:24:33 Ricky Clarkson wrote:
> That's a very good answer to a different question.

He-he... Everybody understands pseudocode own way ;-)


Re: [scala] Small Scala application

by Bill Burdick-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.