Search code examples
scalafunctional-programmingfoldleft

Scala: map+filter instead of foldLeft


Is it possible to substitute the foldLeft function through some combination of map and filter in Scala? For example on this task.

Input is a list of triples (student name, course, grade):

val grades = List(("Hans", "db", 2.3), ("Maria", "prog1", 1.0), ("Maria", "prog2", 1.3), ("Maria", "prog3", 1.7), ("Hans", "prog2", 1.7), ("Josef", "prog1", 1.3), ("Maria", "mathe1", 1.3), ("Josef", "db", 3.3), ("Hans", "prog1", 2.0))

Then to each student, a list of their courses and grades should be mapped. With foldLeft it goes like that:

grades.foldLeft(Map[String, List[(String, Double)]]())((acc, e) => acc + (e._1 -> (acc.getOrElse(e._1, List()) ::: List((e._2, e._3))))).toList

Output:

List[(String, List[(String, Double)])] = List((Hans,List((db,2.3), (prog2,1.7), (prog1,2.0))), (Maria,List((prog1,1.0), (prog2,1.3), (prog3,1.7), (mathe1,1.3))), (Josef,List((prog1,1.3), (db,3.3))))

How to achieve the same output using only map and filter functions? So far i have this, but the output is slightly different.

grades.map(x => (x._1, List())).distinct.flatMap(x => grades.map(z => if(!x._2.contains(z._2, z._3)) (x._1, x._2 ::: List((z._2, z._3)))))

Solution

  • grades
      .map({ case (name, _, _) => name })
      .distinct
      .map(name => {
        val scores = grades
          .filter({ case (nameInternal, _, _) => name == nameInternal })
          .map({ case (_, subject, score) => (subject, score) })
        (name, scores)
      })
    
    // res0: List[(String, List[(String, Double)])] = List((Hans,List((db,2.3), (prog2,1.7), (prog1,2.0))), (Maria,List((prog1,1.0), (prog2,1.3), (prog3,1.7), (mathe1,1.3))), (Josef,List((prog1,1.3), (db,3.3))))