【问题标题】:scala: Specifying method type for curryingscala:指定柯里化的方法类型
【发布时间】:2018-01-28 08:46:15
【问题描述】:

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

  type FTDataReductionProcess =(PipelineConfiguration,RDDLabeledPoint) => RDDLabeledPoint

以上工作正常。

但我想做的是不同的。我想先将配置添加到方法中,然后再添加数据。所以我需要咖喱。

我认为这会起作用:

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

但这会导致编译错误

无法解析符号 RDDLabeledPoint

任何建议将不胜感激

【问题讨论】:

    标签: scala currying


    【解决方案1】:

    您需要一个函数,该函数接受 PipelineConfiguration 并返回另一个函数,该函数接受 RDDLabeledPoint 并返回 RDDLabeledPoint

    • 什么是域? PipelineConfiguration
    • 什么是返回类型?一个“接受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
    

    【讨论】:

    • 一如既往,我的想法有问题。全新思维方式的函数式编程
    • 如果您简要了解一下 Haskell,这种语法会显得更加自然。在 Haskell 中,您通常会像这样声明多参数函数:foo :: a -> b -> c -> returnType
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2018-01-24
    • 2021-02-07
    • 1970-01-01
    • 2021-03-11
    • 2011-11-03
    • 2013-07-20
    相关资源
    最近更新 更多