-
-
Save egnha/8b4d2b15e8c33056a2d8cbabbcb1c966 to your computer and use it in GitHub Desktop.
Code for comment to https://xianblog.wordpress.com/2010/09/13/simply-start-over-and-build-something-better
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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