在scala中调用函数参数

时间:2014-01-03 11:40:58

标签: scala

在scala中,我看到一些函数将另一个函数作为参数。有人能告诉我如何调用该功能。

如果

function A needs a function as a parameter.

I pass B as an argument to function A.

有人可以告诉我how I can invoke, or use the B, inside A function

1 个答案:

答案 0 :(得分:4)

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