## A function that increments its argument in place:
inc <- function(x){
modify_in_place(x, x+1)
}
y <- 1
z <- 1
stopifnot(inc(z) == 2)
stopifnot(z == 2)
stopifnot(inc(y) == 2)
stopifnot(y == 2)
stopifnot(inc(z) == 3)
stopifnot(z == 3)
stopifnot(inc(identity(z)) == 4)
stopifnot(z == 3) # Not updated!
## Modify an argument that's been updated in place:
inc2 <- function(y){
y <- y + 1
modify_in_place(y)
}
z
stopifnot(inc2(z) == 4)
stopifnot(z == 4)
## Decrement the first argument, increment the second:
incdec <- function(x,y){
modify_in_place(x, x-1)
modify_in_place(y, y+1)
}
c(y,z)
incdec(y,z)
stopifnot(all(c(y,z) == c(1,5)))
Run the code above in your browser using DataLab