testing2/danube.scala
changeset 266 ca48ac1d3c3e
parent 249 1997cfcd6334
child 284 9a04eb6a2291
--- a/testing2/danube.scala	Sat Jun 22 08:39:52 2019 +0100
+++ b/testing2/danube.scala	Wed Jul 24 14:22:06 2019 +0100
@@ -124,14 +124,23 @@
 // (5) Implement a suggestions function which takes a rating
 // map and a movie_name as arguments. It calculates all the recommended
 // movies sorted according to the most frequently suggested movie(s) first.
+
+// needed in Scala 2.13.
+ 
+def mapValues[S, T, R](m: Map[S, T], f: T => R) =
+  m.map { case (x, y) => (x, f(y)) }
+
 def suggestions(recs: Map[String, List[String]], 
                     mov_name: String) : List[String] = {
   val favs = favourites(recs, mov_name).flatten
-  val favs_counted = favs.groupBy(identity).mapValues(_.size).toList
+  val favs_counted = mapValues(favs.groupBy(identity), (v:List[String]) => v.size).toList
   val favs_sorted = favs_counted.sortBy(_._2).reverse
   favs_sorted.map(_._1)
 }
 
+// check
+// groupMap is equivalent to groupBy(key).mapValues(_.map(f))
+
 // test cases
 
 //suggestions(ratings_map, "912")