I am new to Scala. I have come across this Scala program and it was asked in interview.
What is the output of the following program
object ApplyTo extends Application {
val k = "gWU`UAXYjT[ig\\\\eBWca";
println(k map {c => (c - k.size).toChar toLower})
}
Please help. I am very new to Scala. I am also not sure that this program will even compile or not.
The output is
scala-dev@gushhq.comMap applies an operation to every member of an iterable. In this case for every character in the string k, the length of the string is subtracted (in this case 20). When you subtract an int from a char you get an Int which is why toChar is needed. Finally toLower ensures that the output is all lowercase. It seems they are just testing whether you are comfortable with the map function, since the rest is pretty straightforward (assuming they don’t want you to memorize ascii codes…)
Also, one of the best features of scala is the interactive console, you may want to try it 🙂