我想写一个函数apply_tuple
,这样:
apply_tuple(new Tuple(x1, x2, ... , xn), f)
回报:
f(x1, x2, ... , xn);
我怎样才能在 groovy 中做到这一点?
我想写一个函数apply_tuple
,这样:
apply_tuple(new Tuple(x1, x2, ... , xn), f)
回报:
f(x1, x2, ... , xn);
我怎样才能在 groovy 中做到这一点?
def apply_tuple(Tuple t, Closure f) {
// inject is just a groovy name for the typical 'fold'
Closure argumentsApplied = t.inject(f) { Closure c, Object tx ->
c.curry(tx)
}
argumentsApplied()
}
或者,如果您的意思不是已经评估f
,而是对结果函数感兴趣,那么只需删除()
. argumentsApplied
这是一个更短的版本(但不太可读的恕我直言),它就是这样做的:
def apply_tuple(t, f) {
t.inject(f) { c, o -> c.curry(o) }
}