Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

scala - Using 'case' in PairRDDFunctions.reduceByKey()

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...