在 Scala 中省略 map 函数的输入数据

Omit input data of map function in Scala

正在学习Spark源码,对下面的代码感到困惑:

/**
 * Return a new RDD containing the distinct elements in this RDD.
 */
def distinct(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T] =
  map(x => (x, null)).reduceByKey((x, y) => x, numPartitions).map(_._1)

map(x => (x, null)) 函数的输入数据是什么?为什么以及何时可以省略输入?

更新:

这里是link源代码。

地图函数map(x => (x, null))map defined by the class

我不明白你关于省略输入的问题。你不能在 scala 中调用一个需要参数而不给它参数的函数。

distinctmap 都是 RDD class (source) 上的方法,所以 distinct 只是在同一个 RDD 上调用另一个方法.

map 函数是一个高阶函数 - 即它接受一个函数作为其参数之一 (f: T => U)

/**
 * Return a new RDD by applying a function to all elements of this RDD.
 */
def map[U: ClassTag](f: T => U): RDD[U] = withScope {
  val cleanF = sc.clean(f)
  new MapPartitionsRDD[U, T](this, (context, pid, iter) => iter.map(cleanF))
}

distinct的情况下,参数fmap是匿名函数x => (x, null).

这是一个在 Scala REPL 中使用匿名函数 (lambda) 的简单示例(在 Scala 列表而不是 Spark RDD 上使用类似的 map 函数):

scala> List(1,2,3).map(x => x + 1)
res0: List[Int] = List(2, 3, 4)