Extension brother code found in many places have used the: _ *, explore what happens today.
1. vararg
example defines a method of variable length parameter sum, and then calculates 1-5, can be written as
-
scala> def sum(args: Int*) = {
-
| was result = 0
-
| for (arg <- args) result += arg
-
| result
-
| }
-
sum: (args: Int*)Int
-
-
scala> val s = sum(1,2,3,4,5)
-
s: Int = 15
But if you use this way will error
-
scala> val s = sum( 1 to 5)
-
<console>: 12: error: type mismatch;
-
found : scala.collection.immutable.Range.Inclusive
-
required: Int
-
val s = sum( 1 to 5)
-
^
In this case must be written on the back: 1 to 5 _ * The parameter sequence converted to
-
scala> val s = sum( 1 to 5: _*)
-
s: Int = 15
2. Variable declaration mode
For example, the following code arr respectively in the first and second values into first and second
scala> val arr = Array(1,2,3,4,5)
arr: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val Array(1, 2, _*) = arr
scala> val Array(first, second, _*) = arr
first: Int = 1
second: Int = 2
Project code
addColumns.foreach(column => {
if (column.udf != null && column.inputColumns != null) {
result = result.withColumn(column.name, column.udf(column.inputColumns.map(col): _*))
} else if (column.expression != null) {
result = result.withColumn(column.name, expr(column.expression))
}
}
)
inputColumns是一个List,
inputColumns.map (col): _ * flattened into a sequence parameter passed to a function udf