Skip to content

Instantly share code, notes, and snippets.

@egnha

egnha/comment.R Secret

Last active September 18, 2017 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egnha/8b4d2b15e8c33056a2d8cbabbcb1c966 to your computer and use it in GitHub Desktop.
Save egnha/8b4d2b15e8c33056a2d8cbabbcb1c966 to your computer and use it in GitHub Desktop.
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