Why does this code result in the compilation error
type mismatch; found : (Int, Char) required:
scala.collection.GenTraversableOnce[?]
?
val n = Map(1 -> 'a', 4 -> 'a')
def f(i: Int, c: Char) = (i -> c)
n.flatMap (e => f(e._1, e._2))
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
map()instead:flatMap()assumes you are returning a collection of values rather than a single element. Thus these would work:The second example is interesting. For each [key, value] pair we return two pairs which are then merged, so the result is: