Skip to content

Instantly share code, notes, and snippets.

@egnha

egnha/comment.R Secret

Last active Sep 18, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.