带参数的Scala传递函数 [英] Scala Passing Function with Argument

查看:947
本文介绍了带参数的Scala传递函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 对象定时器{
def oncePerSecond(callback:(Int)=> Unit){
while(true){callback(x);线程睡眠1000}
}
def timeFlies(x:int){
println(time flies like a arrow ...)
}
def main args:Array [String]){
oncePerSecond(timeFlies(5))
}
}

我怎样才能使上面的代码工作?编辑:我在一秒中添加了一个x来澄清目标是传递整数。

解决方案

至少有两种方法可以做到这一点,具体取决于您想要传递参数的位置。第一种方法是保存 main ,就像你拥有它。

  object定时器{
def oncePerSecond(callback:=> Unit){
while(true) { 回电话;线程睡眠1000

def timeFlies(x:Int){
println(time flies like a arrow ...)
}
def main( args:Array [String]){
oncePerSecond(timeFlies(5))
}
}

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

 对象定时器{
def oncePerSecond(callback:(Int)=> Unit){
val x = 5
while(true){callback(x);线程睡眠1000

def timeFlies(x:Int){
println(time flies like a arrow ...)
}
def main( args:Array [String]){
oncePerSecond(timeFlies)
}
}

请注意, timeFlies 的签名(Int)=>单元,但 timeFlies(5)具有签名 => Unit ,因为部分申请。这基本上意味着您可以应用该参数来自动创建一个占用较少参数的函数。 oncePerSecond 需要知道其签名是否已经将 Int 参数应用于回调。

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


The Scala example for passing a function to another function lacks the case where the passed function (timeFlies) takes an argument (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.

解决方案

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 main like you had it.

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 timeFlies has 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. oncePerSecond needs to know in its signature if you've already applied the Int parameter to the callback or not.

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

这篇关于带参数的Scala传递函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆