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.
Scala - Functions with Named Arguments Named arguments allow you to pass arguments to a function in a different order. The syntax is simply that each argument is preceded by a parameter name and an equals sign. Try the following program, it is a simple example to show the functions with named arguments.
=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .
Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.
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.
The return type of timeFlies
will be Unit
, not Function
. Perhaps you meant:
oncePerSecond(() => timeFlies(5))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With