No-op function as conditional replacement for stopifnot ()

Is there a no-op function in R, so even if the parameters are expensive, does it return immediately? I am looking for conditional replacement function stopifnot.

> noop(runif(1e20))
# returns immediately and uses no memory
+5
source share
1 answer

I think this would do:

noop <- function(...) invisible(NULL)

how lazy appreciation comes to the rescue here:

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> 

therefore, even when we increase N, no increase in execution time is visible.

+9
source

All Articles