用阶乘函数 n! = n * (n-1)! 作为例子:
@FunctionalInterface
static
interface
IntFunc {
int
apply(
int
n);
}
public
static
void
main(String[] args) {
IntFunc factor = n -> n <=
0
?
1
: n * factor.apply(n -
1
);
System.out.println(factor.apply(
10
));
// Expect: 3628800
}
|
其实这段代码是无法编译通过的。那么如何才能用Lambda实现递归呢?
修改一下: