Cristina Kundalini Cristina Kundalini - 4 months ago 25
Scala Question

How can I modify each element of a list while iterating over it in Scala?

I have a List of String declared like this:

var re1_emoticons=""::Nil

which I have then filled in with emoticons (Strings).

I am trying to add a
at the beginning of each String in re1_emoticons, modifying the original var.

first attempt)

re1_emoticons.foreach(t=>"""\\""" + t)

second attempt)

re1_emoticons.foreach(t=>re1_emoticons.indexOf(t)="""\\"""+ t)

third attempt)

re1_emoticons.foreach(t=>re1_emoticons.indexOf(t):="""\\"""+ t)

However still don't find the way. Is there a correct way to do this?


Use the map-function. It creates a new list with a function applied to each of the items. When you get the new list, you can assign it to the var you already have.

re1_emoticons = => """\\""" + t)