Is there a no-op function in R so that, even if the parameters would be expensive to evaluate, it returns immediately? I'm looking for a conditional replacement of the stopifnot
function.
> noop(runif(1e20))
# returns immediately and uses no memory
I think this would do:
noop <- function(...) invisible(NULL)
as lazy evaluation 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>
so even when we increase N no runtime increase can be seen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With