I wrote the following simple example to understand how the map method works:
object Main{
def main (args : Array[String]) = {
val test = "abc"
val t = Vector(97, 98, 99)
println(test.map(c => (c + 1))) //1 Vector(98, 99, 100)
println(test.map(c => (c + 1).toChar)) //2 bcd
println(t.map(i => (i + 1))) //3 Vector(98, 99, 100)
println(t.map(i => (i + 1).toChar)) //4 Vector(b, c, d)
};
}
I didn't quite understand why bcd is printed at //2
. Since every String is treated by Scala as being a Seq
I thought that test.map(c => (c + 1).toChar)
should have produced another Seq
. As //1
suggests Vector(b, c, d)
. But as you can see, it didn't. Why? How does it actually work?