Reputation: 9429
In scala, I have seen some functions get another function as parameters. Can someone tell me how I can invoke that function.
If
function A needs a function as a parameter.
and
I pass B as an argument to function A.
Can someone please tell me then how I can invoke, or use the B, inside A function
.
Upvotes: 1
Views: 114
Reputation: 361
scala> def foo[A, R](func: A => R, arg: A): R = func(arg)
foo: [A, R](func: A => R, arg: A)R
scala> def bar(x: Int): Int = x + 1
bar: (x: Int)Int
scala> foo(bar, 42)
res1: Int = 43
scala> def nothing(x: Int): Unit = println("Yo: " + x)
nothing: (x: Int)Unit
scala> foo(nothing, 42)
Yo: 42
Upvotes: 4