0
import java.time.LocalDate
object Main extends App{
    val scores: Seq[Score] = Seq(score1, score2, score3, score4)
    println(getDate(scores)(LocalDate.of(2020, 1, 30))("Alice"))
    def getDate(scoreSeq: Seq[Score]): Map[LocalDate, Map[String, Int]] = scores.groupMap(score => score.date)(score=>Map(score.name -> (score.english+score.math+score.science)))
}

I would like to implement a function that maps the examination date to a map of student names and the total scores of the three subjects on that date, and if there are multiple scores for the same student on the same date, the function returns the one with the highest total score. However, here is the function

found :scala.collection.immutable.Map[java.time.LocalDate,Seq[scala.collection.immutable.Map[String,Int]]]]

"required: Map[java.time.LocalDate,Map[String,Int]]".

How can I resolve this?

2
  • Its like when asked to provide a Car, you actually provided a Cup. And the compiler is telling you that it wanted a Car and not this Cup. Commented Oct 30, 2022 at 14:30
  • How can I improve it? Commented Oct 30, 2022 at 14:38

1 Answer 1

2

The error means what it says: The type of the variable and the type of the value being assigned to the variable don't match. It even tells you what the two types are!

The actual problem is that groupMap isn't returning the type you think it should. The values in the resulting Map are a Seq of Maps rather than a single Map with all the results.

You can use groupMapReduce to convert the List to a single Map by concatenation the Maps using ++:

scores
  .groupMapReduce(_.date)
    (score=>Map(score.name -> (score.english+score.math+score.science)))
    (_ ++ _)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.