Created
July 6, 2012 23:44
-
-
Save hadley/3063436 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
with(x, { | |
f(z) | |
}) | |
# Which of the following statements is this equivalent to? | |
# | |
# a) x$f(x$z) | |
# b) f(x$z) | |
# c) x$f(z) | |
# d) f(z) | |
# | |
# Scroll down for the answer | |
# It depends on what's in x! | |
x <- list() | |
f <- function(x) x + 1 | |
z <- 1 | |
with(x, { | |
f(z) | |
}) | |
x <- list(x = 2) | |
with(x, { | |
f(z) | |
}) | |
x <- list(f = function(x) x + 10) | |
with(x, { | |
f(z) | |
}) | |
x <- list(f = function(x) x + 10, z = 10) | |
with(x, { | |
f(z) | |
}) | |
# This is one reason not to use with - you need to know | |
# exactly what's in x before you can reason about the | |
# result of the code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment