从此链接显示一些Scala示例:http :
//spark.apache.org/docs/latest/mllib-collaborative-filtering.html
map{}和之间有什么区别map()?
val ratings = data.map(_.split(',') match { case Array(user, item, rate) =>
Rating(user.toInt, item.toInt, rate.toDouble)
})
val usersProducts = ratings.map { case Rating(user, product, rate) =>
(user, product)
}
Run Code Online (Sandbox Code Playgroud)
map是一种接受函数作为参数的方法。因此,通常调用map就像调用方法map(aFunction)一样。但是,Scala在语法上提供了很多灵活性/速记方式:
val list = List((1,2),(3,4))
//An ordinary method call. The braces just evaluate to an anonymous function
val a = list.map({ case (a, b) => a + b }) // List(3,7)
// Now lets define a non anonymous function of the same type as above:
def func(x: (Int,Int)) = x match {case (a,b) => a + b}
// Scala allows this readable "infix notation" for single parameter methods
val a = list map func // List(3,7)
//Now to the main point you asked, this syntax uses the same readable form as above
//but uses an anonymous function.
val a = list map {case (a,b) => a + b} // List(3,7)
Run Code Online (Sandbox Code Playgroud)