Code for comment to https://xianblog.wordpress.com/2010/09/13/simply-start-over-and-build-something-better
library(nofrills) | |
x <- 0 | |
# make x local by pegging it to an argument | |
f1 <- fn(x = !!x ~ { | |
if (runif(1) > .5) | |
x <- 10 | |
x | |
}) | |
# assign local x to global x at the time of f2's creation (not call) | |
f2 <- fn(~ { | |
x <- !!x | |
if (runif(1) > .5) | |
x <- 10 | |
x | |
}) | |
# even when x changes in the lexical scope, f1 and f2 don't | |
x <- 1 | |
replicate(10, f1()) # only contains 0's or 10's | |
replicate(10, f2()) # ditto |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment