【发布时间】:2023-03-21 01:26:01
【问题描述】:
我正在尝试重构它。在 Python 中,我会使用装饰器。这样做的'R'tful方法是什么?说,我们有这种模式
good_input <- format_input( bad_input )
bad_output <- use_this_func( good_input )
good_output <- format_output( bad_output )
然后,
good_input <- format_input( bad_input )
bad_output <- use_this_other_func( good_input )
good_output <- format_output( bad_output )
你可以想象,这就像野生蘑菇一样繁殖。我想要接近这个解决方案的东西
use_this_robust_func <- wrapper( use_this_func ) # Or wrapper( use_this_other_func )
good_output <- use_this_robust_func( bad_input )
我正在尝试使用format_input 和format_output 来包装对use_this_func 和use_this_other_func(和相关函数)的调用。部分使用这个question,到目前为止我有
wrapper <- function( func_not_robust ){
func_robust <- function( ... ){
# This is the bit I haven't figured out
... format_input( ) ... # supposed to convert bad input - the function argument - to good
bad_output <- func_not_robust( ... ) # supposed to take good input as argument
good_output <- format_output( bad_output )
return( good_output )
}
return( func_robust )
}
抱歉,伪代码。请注意,我不确定这是在 R 中采用的方式。我不喜欢上面解决方案的草图,它是通过将 Python 翻译成 R 产生的 - 并且非常糟糕 - R 本地人如何做到这一点?提前致谢。
【问题讨论】:
-
能给个实际用例吗?