Using 'case' in PairRDDFunctions.reduceByKey()

风格不统一 提交于 2019-12-17 19:26:18

问题


This is the syntax for method reduceByKey

def reduceByKey(func: (V, V) ⇒ V): RDD[(K, V)] 

In a word count program I am practicing, I see this code,

val counts = words.map(word => (word, 1)).reduceByKey{case (x, y) => x + y}

The application works with (x, y) instead of case(x, y). What is the use of case here. I did also check the answer from @ghik here. but not able to understand


回答1:


Scala supports multiple ways of defining anonymous functions. The "case" version is so called Pattern Matching Anonymous Functions which is more or less equivalent to:

(x: Int, y: Int) => (x, y) match { case (x, y) => x + y }

while version without case is pretty much what it looks like:

(x: Int, y: Int) => x + y

In this case simple _ + _ would be enough though:

val counts = words.map(word => (word, 1)).reduceByKey(_ + _)

Probably the simplest case when you can benefit from using pattern matching is when you deal with Scala Options:

(x: Option[Int], y: Option[Int]) => (x, y) match {
    case (Some(xv), Some(yv)) => xv + yv
    case (Some(xv), _) => xv
    case (_, Some(yv)) => yv
    case _ => 0
} 


来源:https://stackoverflow.com/questions/30875381/using-case-in-pairrddfunctions-reducebykey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!