Reputation: 721
I want to code a Scala function that takes a list of Strings and in each string it duplicates each character and increments it«s value by one like so:
def diplicateAndIncrementChar(l: List[String]): List[String] = {
// (...)
}
diplicateAndIncrementChar(List("ROAD", "BALL", "LEMON"))
//> res50: List[String] = List(RSOPABDE, BCABLMLM, LMEFMNOPNO)
The trick is that I have to do this in a single statement.
I thought about using map and did this:
l.map(s => new String(s.map(c => (c+1)).toString()))
But it doesn't give exactly the result that I want:
//> res10: List[String] = List(Vector(83, 80, 66, 69), Vector(67, 66, 77, 77), Vector(77, 70, 78, 80, 79))
Can anyone help me with this problem?
Upvotes: 0
Views: 289
Reputation: 5315
Since your input and your output are list of chars of different sizes, you probably want to use flatMap
, instead of map
.
By the way (c: Char + 1)
is an Int
, so you'll have to convert it explicitly back to Char
to get what you want.
I won't say more, since this should be enough for you to find out the rest by yourself.
Upvotes: 3