Skip to content

Instantly share code, notes, and snippets.

@krlmlr
Created August 5, 2013 21:56
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 krlmlr/6159962 to your computer and use it in GitHub Desktop.
Save krlmlr/6159962 to your computer and use it in GitHub Desktop.
Test for failing lazy evaluation
.expr_to_fun <- function(x) {
expr <- substitute(x)
res <- force(x)
if (is.function(res)) {
list(f = res, val = res())
} else {
f <- eval.parent(substitute(function() expr))
list(f = f, val = res)
}
}
test <- function() {
require(assertthat)
f <- function(x) .expr_to_fun(x)
inc <- function() i <<- i + 1
i <- 0
f2e <- f(inc())
f2e$f()
print(i)
assert_that(i == 2)
}
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment