forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachevector.R
38 lines (34 loc) · 1.39 KB
/
cachevector.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
makeVector <- function(x = numeric()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setmean <- function(mean) m <<- mean
getmean <- function() m
list(set = set, get = get,
setmean = setmean,
getmean = getmean)
}
cachemean <- function(x, ...) {
m <- x$getmean()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- mean(data, ...)
x$setmean(m)
m
}
crazy <- function() { # create a new environment with a local variable 'x' and access to another variable 'x'
# declared somewhere outside this function
x <- 3.14 # assign the numeric value 3.14 to local variable 'x'
print(x) # output the current value of local variable 'x' (1)
{ print(x); # output the current value of local variable 'x' (2)
x <<- 42; # assign the numeric value 42 to variable 'x' declared outside this function (3)
print(x) # output the current value of local variable 'x' (4)
}
print(x) # output the current value of local variable 'x' (5)
}