加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

实现更高阶函数,在scala中执行currying

发布时间:2020-12-16 19:11:17 所属栏目:安全 来源:网络整理
导读:我的一位同事给我发了一个问题如下: Implement a HOF(higher order function) that performs currying,the signature of your function is as follows: def curry[A,B,C](f:(A,B) = C) : A = B = C Similarly,implement a function that performs uncurryin
我的一位同事给我发了一个问题如下:

Implement a HOF(higher order function) that performs currying,the
signature of your function is as follows:

def curry[A,B,C](f:(A,B) => C) : A => B => C

Similarly,implement a function that performs uncurrying as follows:

def uncurry[A,C](f:A => B => C): (A,B) => C

我理解currying的方式是,如果你有一个带有多个参数的函数,你可以重复将函数应用于每个参数,直到得到结果.

所以f:(A,B)=> C变成A => f(A,_)=> F(B)????

并且可以将这个应用程序整合到一个函数中,如下所示:

f:A => B => C将是f(A,B)?

也许我只是对这里的语法感到困惑,但如果有人能指出我在这里缺少的东西,那将会很棒.

谢谢

解决方法

希望这个带有大量注释的完整工作示例很容易理解.如果您有任何疑问,请回复.

您可以通过将其放入Scala解释器来执行此代码.

// Here's a trait encapsulating the definition your coworker sent.
trait Given {
  def curry[A,B) => C) : A => B => C
  def uncurry[A,B) => C
}

object Impl extends Given {
  // I'm going to implement uncurry first because it's the easier of the
  // two to understand.  The bit in curly braces after the equal sign is a
  // function literal which takes two arguments and applies the to (i.e.
  // uses it as the arguments for) a function which returns a function.
  // It then passes the second argument to the returned function.
  // Finally it returns the value of the second function.
  def uncurry[A,B) => C = { (a: A,b: B) => f(a)(b) }

  // The bit in curly braces after the equal sign is a function literal
  // which takes one argument and returns a new function.  I.e.,curry()
  // returns a function which when called returns another function
  def curry[A,B) => C) : A => B => C = { (a: A) => { (b: B) => f(a,b) } }
}

def add(a: Int,b: Long): Double = a.toDouble + b
val spicyAdd = Impl.curry(add)
println(spicyAdd(1)(2L)) // prints "3.0"
val increment = spicyAdd(1) // increment holds a function which takes a long and adds 1 to it.
println(increment(1L)) // prints "2.0"
val unspicedAdd = Impl.uncurry(spicyAdd)
println(unspicedAdd(4,5L)) // prints "9.0"

一个较少数字的例子怎么样?

def log(level: String,message: String) { 
  println("%s: %s".format(level,message)) 
} 
val spicyLog = Impl.curry(log) // spicyLog's type is String => Unit
val logDebug = spicyLog("debug") // This new function will always prefix the log
                                 // message with "debug".
val logWarn = spicyLog("warn") // This new function will always prefix the log 
                               // message with "warn".
logDebug("Hi,sc_ray!") // prints "debug: Hi,sc_ray!"
logWarn("Something is wrong.") // prints "warn: Something is wrong."

更新您回答问“编译??器如何评估表达式,例如a => b => f(a,b).”嗯,它没有.至少在同事的代码片段中定义的东西是无法编译的.但是,一般来说,如果你看到A =>形式的东西. B => C表示“将A作为参数的函数;它返回一个函数,该函数将B作为参数并返回C.”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读