scala:指定柯里化(Currying)的方法类型

标签 scala currying

以下方法类型描述了将配置信息和数据传递给方法的函数。这种类型的每种方法都可以执行不同的功能。

  type FTDataReductionProcess =(PipelineConfiguration,RDDLabeledPoint) => RDDLabeledPoint

以上工作完成。

但我想做的是不同的。我想先将配置添加到方法中,然后再添加数据。所以我需要柯里化(Currying)。

我认为这会起作用:

  type FTDataReductionProcess =(PipelineConfiguration)(RDDLabeledPoint) => RDDLabeledPoint

但这会导致编译错误

Cannot resolve symbol RDDLabeledPoint

如有任何建议,我们将不胜感激

最佳答案

您需要一个接受PipelineConfiguration 并返回另一个接受RDDLabeledPoint 并返回RDDLabeledPoint 的函数。

  • 域名是什么? 管道配置
  • 返回类型是什么?一个“接受RDDLP并返回RDDLP的函数”,即:(RDDLabeledPoint => RDDLabeledPoint)

一起:

type FTDataReductionProcess = 
  (PipelineConfiguration => (RDDLabeledPoint => RDDLabeledPoint))

由于 => 是右关联的,因此您还可以编写:

type FTDataReductionProcess = 
  PipelineConfiguration => RDDLabeledPoint => RDDLabeledPoint

顺便说一下:函数文字也同样适用,它提供了一个很好的简洁语法。这是一个较短的示例来演示这一点:

scala> type Foo = Int => Int => Double // curried function type
defined type alias Foo

scala> val f: Foo = x => y => x.toDouble / y     // function literal
f: Foo = $$Lambda$1065/1442768482@54089484

scala> f(5)(7)  // applying curried function to two parameters
res0: Double = 0.7142857142857143

关于scala:指定柯里化(Currying)的方法类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481614/

相关文章:

java - Windows : <console>:16: error: not found: value sqlContext 中的 Spark 失败

f# - 反向柯里化(Currying)?

javascript - 将动态参数添加到函数,该函数的参数作为参数传递给当前函数

python - 与 Python fn 模块一起使用的函数

scala - 避免在 Spark 中使用 ReduceByKey 进行混洗

java - Spark Sql 中的poseexplode() 的替代方案是什么,因为它不动态采用可变数量的参数?

scala - 如何在 Scala 中更新数组?

scala - scala 中的函数字面量表达式

functional-programming - Perl 6 - Curried 函数挂起

javascript - 如何执行一个功能,无论它是正常的还是携带的,并产生相同的结果?