Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yield String from List[Char]

I have a l: List[Char] of characters which I want to concat and return as a String in one for loop.

I tried this

val x: String = for(i <- list) yield(i) 

leading to

 error: type mismatch;    found   : List[Char]    required: String 

So how can I change the result type of yield?

Thanks!

like image 949
xyz Avatar asked May 28 '11 15:05

xyz


1 Answers

Try this:

val x: String = list.mkString 

This syntax:

for (i <- list) yield i 

is syntactic sugar for:

list.map(i => i) 

and will thus return an unchanged copy of your original list.

like image 159
Jean-Philippe Pellet Avatar answered Oct 15 '22 04:10

Jean-Philippe Pellet