【问题标题】:What is the difference between map{} and map() [duplicate]map{} 和 map() 有什么区别 [重复]
【发布时间】:2016-12-24 22:33:04
【问题描述】:

从此链接显示一些 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)
}

【问题讨论】:

    标签: scala


    【解决方案1】:
    val a = list.map(_.split(','))
    
    // the above is a shorthand for
    val a = list.map({ case i => i.split(',') })
    
    // Now lets consider this
    val a = list.map { case (a, b) => a + b }
    
    // or this
    val a = list map { case (a, b) => a + b }
    
    // some people like the above ways of writing
    // because they consider of less brackets as cleaner code.
    // but the above two are another way to write
    val a = list.map({ case (a, b) => a + b })
    

    这里要理解的是,在 Scala 中,您可以使用空格而不是 . 来访问实例方法。

    基本上,

    // lets say you have a class A
    case class A(i: Int) {
    
      def merged[B](b: B): String = i.toString + " :: " + b.toString
    
    }
    
    //Now, lets say you have an instance of A
    val a = A(5)
    // and an instance of String
    val s: String = "abcd"
    
    // Now, if you write
    val merged = a merged s
    // it is syntactic sugar for
    val merged = a.merged(s)
    

    同样,List[A] 有一个方法 map[B](f: A => B): List[B]

    val list = List[Int](1, 2, 3)
    
    // so when you write
    val list2 = list map { case i => i + 1}
    
    // its syntactic sugar for,
    val list2 = list.map({ case i => i + 1 })
    
    // There is a lot going on here
    // if we were to do everything step by step
    
    // first you are creating a partial function
    val pf: PartialFunction[Int, Int] = { case i => i + 1 }
    
    // Now, PartialFunction[Int, Int] is a subtype of Int => Int
    // so we can refer to it as a Int => Int
    val f: Int => Int = pf
    
    // Now we pass it as an argument to map
    val list2 = list.map(f)
    

    【讨论】:

    • 做得很好,谢谢
    • 不,第一个不是第二个的简写。第二个甚至没有编译。应该是a => a.split(',')
    • Alexey 所说的还有很多。 {} 表示将提供匿名函数体作为输入,几乎与速记无关。它通常不受map 方法甚至更高阶函数的约束。
    • @sebszyller 你明白吗,在 scala 中你可以使用空格而不是 . 来访问实例方法。所以基本上任何表达式a abc b,其中b 是某个类B 的实例,a 是类A 的实例,具有abc[B](b: B) 方法实际上是a.abc(b) 的语法糖。
    • 当然可以,但与{}() 有什么关系?
    【解决方案2】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 2023-03-04
      • 2014-03-15
      • 2018-03-08
      • 1970-01-01
      • 2019-04-01
      • 2011-07-05
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多