Apache-Spark : What is map(_._2) shorthand for?

后端 未结 5 1001
予麋鹿
予麋鹿 2020-12-28 08:59

I read a project\'s source code, found:

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

inputMBR is a tuple.

the function

相关标签:
5条回答
  • 2020-12-28 09:30

    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.

    0 讨论(0)
  • 2020-12-28 09:31

    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)
    
    0 讨论(0)
  • 2020-12-28 09:36

    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) {...}

    0 讨论(0)
  • 2020-12-28 09:45

    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).

    0 讨论(0)
  • 2020-12-28 09:50

    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?.

    0 讨论(0)
提交回复
热议问题