views:

166

answers:

5

Hi all,

I have a list in scala called l : List[AType] that I want to change to list[String].

This may sound like a very dirty, inefficient approach, but I'm not quite sure of the best way to do this. My code was:

var result = new Array[String]("a","b")
l foreach { case a => result = result :+ (a.toString().toUpperCase()); }
result toList

I'm not sure if this is where my mistake is, because it's not giving me anything, it's not even printing anything even if I put a print statement inside the loop.

So I decided to change this to a more imperative way:

for(i <- 0 to l.length) {
    result.update(i, l(i).toString)
}

This time I see things that I want to see when printing inside the loop, but at the end the program crashed with an IndexOutOfBound error.

Is there any more efficient and better way to do this?

Thanks!

A: 

How about

for(i <- 0 to l.length-1) {
    result.update(i, l(i).toString)
}
Eton B.
it works, thanks, but i'm looking at more of a scala way to do it rather than java/c++ way...
the_great_monkey
`result.update(i, l(i).toString)` is slightly better written `result(i) = l(i).toString`, but this is pretty bad Scala code and works only when `result` is a mutable class and is efficient only when it and `l` are indexed sequences. All it all, it would never qualify as idiomatic Scala.
Randall Schulz
+12  A: 

Take a look at the map function. For example,

scala> List("Some", "Strings").map(_.toUpperCase)
res2: List[java.lang.String] = List(SOME, STRINGS)

or

scala> List("Some", "Strings").map(_.length)
res0: List[Int] = List(4, 7)
sluukkonen
What about if it's from another type? Do I say l.map(_.toString).map(_.toUpperCase) or l.map(_.toString._toUpperCase) ? The latter didn't work for me...
the_great_monkey
@the_great_monkey then it is `l.map(_.toString.toUpperCase)`
Moritz
`l.map(.toString).map(.toUpperCase)` is not valid Scala. `l.map(_.toString.toUpperCase)` works. You had a spurious _ between the dot and the toUpperCase.
Randall Schulz
ahh, okay great, thanks guys. Can you point out a resource where I can learn about this thing? I think I'm missing out all this time because I haven't used map functions at all!
the_great_monkey
Any introduction to Scala will cover mapping, reducing, folding, and higher-order functions (HOFs) in general. There's already gobs of material on Scala, but http://www.scala-lang.org/node/1305 is a good place to start.
Randall Schulz
To clarify, `l.map(_.toString)` is a shorter way to write `l.map(x => x.toString)`.
sluukkonen
This is the right answer (in that it mentions map), but ideally the example should change the type of the list, e.g. `List("some", "strings").map(_.length):List[Int]`
pelotom
@pelotome Even more ideally, the example should change the type of the list TO String, e.g. List(1,1,2,3,5).map(_.toString)
ziggystar
+7  A: 

Just a remark on the for loop. Here are two correct ways of doing that loop:

// Using "until" instead of "to": a until b == a to (b - 1)
for(i <- 0 until l.length) {
    result.update(i, l(i).toString)
}

// Using "indices" to get the range for you
for(i <- l.indices) {
    result.update(i, l(i).toString)
}
Daniel
+1 I didn't know that one!
retronym
+3  A: 
 def f(s:String) = s.toCharArray // or output something else of any type 
 val l = List("123", "234", "345")
 l.map(f)
Jus12
`l map f` is much better
Jus12
+1  A: 

Did you try for-comprehensions?

val result=for(elem <- l) yield elem.toString().toUpperCase();
jpalecek