Apache-Spark: what is a map (_._ 2)?

I read the source code of the project, found:

val sampleMBR = inputMBR.map(_._2).sample 

inputMBR is a tuple.

map function definition:

 map[U classTag](f:T=>U):RDD[U] 

it seems that map(_._2) is a shorthand for map(x => (x._2)) .

Can anyone tell me the rules of these abbreviations?

+7
scala apache-spark
source share
5 answers

Syntax _ can be a bit confusing. When _ is used alone, it represents an argument in an anonymous function. Therefore, if we work on pairs: map(_._2 + _._2) will be shortened for map(x, y => x._2 + y._2) . When _ is used as part of a function name (or value name), it does not really matter. In this case, x._2 returns the second element of the tuple (assuming x is a tuple).

+11
source share

collection.map (_._ 2) emits the second component of the tuple. An example from pure Scala (Spark RDD work the same):

 scala> val zipped = (1 to 10).zip('a' to 'j') zipped: scala.collection.immutable.IndexedSeq[(Int, Char)] = Vector((1,a), (2,b), (3,c), (4,d), (5,e), (6,f), (7,g), (8,h), (9,i), (10,j)) scala> val justLetters = zipped.map(_._2) justLetters: scala.collection.immutable.IndexedSeq[Char] = Vector(a, b, c, d, e, f, g, h, i, j) 
+6
source share

The two underscores in ' _._2 ' are different.

The first ' _ ' for the placeholder of the anonymous function; The second ' _2 ' is a member of the case Tuple class.

Something like:

class case Tuple3 ( _1 : T1, _2 : T2, _3 : T3) {...}

+3
source share

I have found a solution.

First underscore here as a placeholder .

To make the function literal even more concise, you can use underscores as placeholders for one or more parameters if each parameter appears only once in the functional literal.

Learn more about Scala's underscore in What is using underscore in Scala? .

0
source share

The first "_" refers to what it is attached to, and since the displayed one is a tuple, you can call any function inside the tuple, and one of the methods is "_2", so it is said below that the second attribute is entered into it.

0
source share

All Articles