无操作函数作为stopifnot()的条件替换

krl*_*mlr 6 r function

R中是否存在无操作功能,即使参数评估成本昂贵,也会立即返回?我正在寻找条件替换stopifnot功能.

> noop(runif(1e20))
# returns immediately and uses no memory
Run Code Online (Sandbox Code Playgroud)

Dir*_*tel 10

我想这会做:

noop <- function(...) invisible(NULL)
Run Code Online (Sandbox Code Playgroud)

懒惰的评价来到这里救援:

R> system.time(replicate(1e4, noop(runif(1e2))))
   user  system elapsed 
   0.01    0.00    0.01 
R> system.time(replicate(1e4, noop(runif(1e5))))
   user  system elapsed 
   0.01    0.00    0.02 
R> system.time(replicate(1e4, noop(runif(1e8))))
   user  system elapsed 
   0.01    0.00    0.01 
R> system.time(replicate(1e4, noop(runif(1e11))))
   user  system elapsed 
   0.01    0.00    0.01 
R> 
Run Code Online (Sandbox Code Playgroud)

所以即使我们增加N,也没有看到运行时增加.