I'm doing a course in Coursera about Scala functional programming. I'm in the 6th week.
I have the following code:
/* define the map of numbers to letters */
val nmem = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ"
)
/* invert the map to get a map of letters to digits */
val charCode: Map[Char, Char] =
for {
(digit, str) <- nmem
ltr <- str
} yield ltr -> digit
My question is how does the for-comprehension work? nmem
introduces the key (char) and the value (string) into digit and str. And later we have: ltr <- str which I don't know how it works because I don't understand how the program knows that ltr is a char instead of an string.
Thank you in advance.