1
def rankLangs(langs: List[String], rdd: RDD[WikipediaArticle]): List[(String, Int)] = langs.map(lang => (lang, occurrencesOfLang(lang ,rdd))).sortBy(-_._2)
def rankLangs(langs: List[String], rdd: RDD[WikipediaArticle]): List[(String, Int)] = langs.map(_ => (_, occurrencesOfLang(_, rdd))).sortBy(-_._2).reverse

The IDE says no error for the first one, but second one is marked as error. Why I could not replace lang as _?

Bostonian
  • 615
  • 7
  • 16
  • 3
    `_ =>` means "ignore the in-parameter", "don't give it a name", "just throw it away", "it won't be used". – jwvh Nov 09 '19 at 23:22

1 Answers1

4

This is since in the second you're using '_' inside the definition of the passing anonymous function.

Thus,

langs.map(_ => (_ , occurrencesOfLang(_,rdd)))

will expand to something like

langs.map(x$1 => (x$2:Any) => (x$2, occurrencesOfLang(x$2,rdd))

thus will return List[String=> (Any) => (String,Int)] instead of List[(String,Int)]

This is because it is equivalent to writing

langs.map(x => (_:Any) => (_, occurrencesOfLang(_,rdd))

As explained here: What are the rules to govern underscore to define anonymous function?

A much cleaner explanation with a suitable example can be found here: scala passing function with underscore produces a function not a value

Ronak Jain
  • 3,073
  • 1
  • 11
  • 17