I read a project\'s source code, found:
val sampleMBR = inputMBR.map(_._2).sample
inputMBR
is a tuple.
the function
The first '_' is referring what is mapped to and since what is mapped to is a tuple you might call any function within the tuple and one of the method is '_2' so what below tells us transform input into it's second attribute.
collection.map(_._2) emits a second component of the tuple. Example from pure Scala (Spark RDDs work the same way):
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)
Two underscores in '_._2
' are different.
First '_
' is for placeholder of anonymous function; Second '_2
' is member of case class Tuple
.
Something like:
case class Tuple3 (_1: T1, _2: T2, _3: T3) {...}
The _ syntax can be a bit confusing. When _ is used on its own it represents an argument in the anonymous function. So if we working on pairs:
map(_._2 + _._2)
would be shorthand for map(x, y => x._2 + y._2)
. When _ is used as part of a function name (or value name) it has no special meaning. In this case x._2
returns the second element of a tuple (assuming x is a tuple).
I have found the solutions.
First the underscore
here is as placeholder.
To make a function literal even more concise, you can use underscores as placeholders for one or more parameters, so long as each parameter appears only one time within the function literal.
See more about underscore
in Scala at What are all the uses of an underscore in Scala?.