scala 带参数的Scala传递函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27433384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:47:08  来源:igfitidea点击:

Scala Passing Function with Argument

scalafunctional-programming

提问by BAR

The Scala example for passing a function to another function lacks the case where the passed function (timeFlies) takes an argument (x).

将函数传递给另一个函数的 Scala 示例缺少传递函数 (timeFlies) 接受参数 (x) 的情况。

object Timer {
  def oncePerSecond(callback: (Int) => Unit) {
    while (true) { callback(x); Thread sleep 1000 }
  }
  def timeFlies(x: int) {
    println("time flies like an arrow...")
  }
  def main(args: Array[String]) {
    oncePerSecond(timeFlies(5))
  }
}

How can I make the above code work?

我怎样才能使上面的代码工作?

Edit: I added an x in the oncepersecond to clarify the goal is to pass the integer.

编辑:我在每秒一次中添加了一个 x 以阐明目标是传递整数。

回答by Karl Bielefeldt

There are at least two ways you can do it, depending on where exactly you want to pass the argument in. The first way is where you keep mainlike you had it.

至少有两种方法可以做到这一点,具体取决于您想要将参数传入的确切位置。第一种方法是保持main原样。

object Timer {
  def oncePerSecond(callback: => Unit) {
    while (true) { callback; Thread sleep 1000 }
  }
  def timeFlies(x: Int) {
    println("time flies like an arrow...")
  }
  def main(args: Array[String]) {
    oncePerSecond(timeFlies(5))
  }
}

The other method is to pass the parameter in at the point of the callback, like this:

另一种方法是在回调点传入参数,如下所示:

object Timer {
  def oncePerSecond(callback: (Int) => Unit) {
    val x = 5
    while (true) { callback(x); Thread sleep 1000 }
  }
  def timeFlies(x: Int) {
    println("time flies like an arrow...")
  }
  def main(args: Array[String]) {
    oncePerSecond(timeFlies)
  }
}

Note that timeFlieshas the signature (Int) => Unit, but timeFlies(5)has the signature => Unit, because of partial application. This basically means you can apply the parameter to automatically create a function that takes fewer parameters. oncePerSecondneeds to know in its signature if you've already applied the Intparameter to the callback or not.

注意timeFlies有签名(Int) => Unit,但timeFlies(5)有签名=> Unit,因为偏应用。这基本上意味着您可以应用参数来自动创建一个参数更少的函数。 oncePerSecond需要在其签名中知道您是否已经将Int参数应用于回调。

Both methods are useful for different use cases. The first way lets oncePerSecondnot have to know about the callback's parameters. The second way lets you change the value of xevery time through the loop if you want.

这两种方法都适用于不同的用例。第一种方法让我们oncePerSecond不必知道回调的参数。如果需要,第二种方法可让您x通过循环更改每次的值。

回答by John

The return type of timeFlieswill be Unit, not Function. Perhaps you meant:

timeFlieswill的返回类型是Unit, not Function。也许你的意思是:

oncePerSecond(() => timeFlies(5))

回答by Ryoichiro Oka

oncePerSecond(() => timeFlies(5))

oncePerSecond(() => timeFlies(5))

or

或者

def oncePerSecond(callback: => Unit) {...

def oncePerSecond(callback: => Unit) {...

回答by Michael Zajac

The parameter callback: () => Unitis a zero-parameter function that returns Unit. You should make it call-by-name parameter (something that evaluates to Unit):

参数callback: () => Unit是一个零参数函数,返回Unit。您应该使其按名称调用参数(计算结果为Unit):

def oncePerSecond(callback: => Unit) = ...