Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 函数调用后的括号_Scala_Apache Spark - Fatal编程技术网

Scala 函数调用后的括号

Scala 函数调用后的括号,scala,apache-spark,Scala,Apache Spark,我是Scala新手,正在阅读这段代码 我知道第一个val-bucketizers是一个变量函数,val-newCols称为bucketizers 但是,我不明白为什么在bucketizers(idx)(filteredataset(inputCol).cast(DoubleType))中,在函数调用bucketizers(idx)之后有一个括号。我检查了一些高级主题,包括闭包、高阶函数和链函数调用,但我认为我没有找到答案 问题:这个括号叫什么?我们能用另一种方式重写它吗 代码: 顾名思义,buc

我是Scala新手,正在阅读这段代码

我知道第一个
val-bucketizers
是一个变量函数,
val-newCols
称为
bucketizers

但是,我不明白为什么在
bucketizers(idx)(filteredataset(inputCol).cast(DoubleType))
中,在函数调用
bucketizers(idx)
之后有一个括号。我检查了一些高级主题,包括闭包、高阶函数和链函数调用,但我认为我没有找到答案

问题:这个括号叫什么?我们能用另一种方式重写它吗

代码:


顾名思义,bucketizers是一系列函数

这是一个非常简单的模式:

scala> val f1 = (i: Int) => 2 * i
f1: Int => Int = <function1>

scala> val f2 = (i: Int) => 3 + i
f2: Int => Int = <function1>

scala> val funs: Seq[Function1[Int,Int]] = Seq (f1, f2)
funs: Seq[Int => Int] = List(<function1>, <function1>)

scala> (0 to 1).map (idx=> 
       funs(idx) (Math.max (7, 8)))
res74: scala.collection.immutable.IndexedSeq[Int] = Vector(16, 11)
scala>valf1=(i:Int)=>2*i
f1:Int=>Int=
scala>valf2=(i:Int)=>3+i
f2:Int=>Int=
scala>val funs:Seq[Function1[Int,Int]]=Seq(f1,f2)
funs:Seq[Int=>Int]=列表(,)
scala>(0到1).map(idx=>
funs(idx)(Math.max(7,8)))
res74:scala.collection.immutable.IndexedSeq[Int]=Vector(16,11)

在集合funs中,调用索引为idx的集合。参数列表如下所示。

bucketizers是一系列函数,顾名思义

这是一个非常简单的模式:

scala> val f1 = (i: Int) => 2 * i
f1: Int => Int = <function1>

scala> val f2 = (i: Int) => 3 + i
f2: Int => Int = <function1>

scala> val funs: Seq[Function1[Int,Int]] = Seq (f1, f2)
funs: Seq[Int => Int] = List(<function1>, <function1>)

scala> (0 to 1).map (idx=> 
       funs(idx) (Math.max (7, 8)))
res74: scala.collection.immutable.IndexedSeq[Int] = Vector(16, 11)
scala>valf1=(i:Int)=>2*i
f1:Int=>Int=
scala>valf2=(i:Int)=>3+i
f2:Int=>Int=
scala>val funs:Seq[Function1[Int,Int]]=Seq(f1,f2)
funs:Seq[Int=>Int]=列表(,)
scala>(0到1).map(idx=>
funs(idx)(Math.max(7,8)))
res74:scala.collection.immutable.IndexedSeq[Int]=Vector(16,11)
在集合funs中,调用索引为idx的集合。参数列表如下所示。

逐步:

  • 变量
    bucketizers
    保存类型为
    Seq[UserDefinedFunction]
    的序列
  • 因此,对于每个索引
    idx:Int
    ,表达式
    bucketizers(idx)
    是一个
    UserDefinedFunction
  • 如果
    f
    是一个
    UserDefinedFunction
    ,并且
    x
    是一个合适的参数,那么可以调用传递
    x
    的函数,如下所示:
    f(x)
  • 例如,您可以使用
    x=filteredDataset(inputCol).cast(DoubleType)
这里再次将所有部件组合为ASCII艺术:

bucketizers(idx)(filteredDataset(inputCol).cast(DoubleType))
\______________/^\________________________________________/^
  function      |               argument                   |
                |                                          |
           open parens for argument                   close parens

下面是一个只使用基本语法的玩具示例:

val inputColumns = List(
  List(1, 2, 3, 4, 5, 6),   // first "column"
  List(4, 6, 5, 7, 12, 15)  // second "column"
)

type UDF = Function[List[Int], List[Int]]

val bucketizers: Seq[UDF] = List(
  _.map(_ * 42),              // first "UDF"
  _.map(_  % 3)               // second "UDF"
)

println(bucketizers(0)(List(1, 100))) // 42, 4200
println(bucketizers(1)(List(1,2,3,4))) // 1,2,0,1

val modifiedTable = inputColumns.zipWithIndex.map{ case (col, idx) =>
  bucketizers(idx)(col)
}

println(modifiedTable)
// Output:
// List(
//   List(42, 84, 126, 168, 210, 252), // first column times 42
//   List(1, 0, 2, 1, 0, 0)            // second column modulo 3
// )
这里,我们声明一个包含两列的“表”和一个函数列表。然后我们将每个函数应用于相应的列。

逐步:

  • 变量
    bucketizers
    保存类型为
    Seq[UserDefinedFunction]
    的序列
  • 因此,对于每个索引
    idx:Int
    ,表达式
    bucketizers(idx)
    是一个
    UserDefinedFunction
  • 如果
    f
    是一个
    UserDefinedFunction
    ,并且
    x
    是一个合适的参数,那么可以调用传递
    x
    的函数,如下所示:
    f(x)
  • 例如,您可以使用
    x=filteredDataset(inputCol).cast(DoubleType)
这里再次将所有部件组合为ASCII艺术:

bucketizers(idx)(filteredDataset(inputCol).cast(DoubleType))
\______________/^\________________________________________/^
  function      |               argument                   |
                |                                          |
           open parens for argument                   close parens

下面是一个只使用基本语法的玩具示例:

val inputColumns = List(
  List(1, 2, 3, 4, 5, 6),   // first "column"
  List(4, 6, 5, 7, 12, 15)  // second "column"
)

type UDF = Function[List[Int], List[Int]]

val bucketizers: Seq[UDF] = List(
  _.map(_ * 42),              // first "UDF"
  _.map(_  % 3)               // second "UDF"
)

println(bucketizers(0)(List(1, 100))) // 42, 4200
println(bucketizers(1)(List(1,2,3,4))) // 1,2,0,1

val modifiedTable = inputColumns.zipWithIndex.map{ case (col, idx) =>
  bucketizers(idx)(col)
}

println(modifiedTable)
// Output:
// List(
//   List(42, 84, 126, 168, 210, 252), // first column times 42
//   List(1, 0, 2, 1, 0, 0)            // second column modulo 3
// )

这里,我们声明一个包含两列的“表”和一个函数列表。然后我们将每个函数应用到相应的列。

太好了!在本例中,
seqOfSplits.zipWithIndex
implicit声明了整个参数列表?否,在本例中是显式的:隐式声明始终使用关键字
Implicit
。val bucketizers=。。。是一个值定义。该代码已经执行并创建了一个Seq[UserDefinedFunction]…-啊,请稍等-我错了,idx是一个索引到Seq。将删除我的错误帖子。对不起,太好了!在本例中,
seqOfSplits.zipWithIndex
implicit声明了整个参数列表?否,在本例中是显式的:隐式声明始终使用关键字
Implicit
。val bucketizers=。。。是一个值定义。该代码已经执行并创建了一个Seq[UserDefinedFunction]…-啊,请稍等-我错了,idx是一个索引到Seq。将删除我的错误帖子。对不起,你为什么反复使用“无父母”这个词?谁没有父母?我见过一些高阶函数和闭包,它们都是在幸福的大家庭中长大的,没有一个是“无父母的”。这个标题以一种悲伤和怪异的方式很奇怪。我是指括号:是啊,说真的,谢谢你修改这个标题,它看起来真的有点奇怪&悲伤;你为什么反复使用“无父母”这个词?谁没有父母?我见过一些高阶函数和闭包,它们都是在幸福的大家庭中长大的,没有一个是“无父母的”。这个标题以一种悲伤和怪异的方式很奇怪。我是指括号:是啊,说真的,谢谢你修改这个标题,它看起来真的有点奇怪&悲伤;D