forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachevector.R
More file actions
62 lines (54 loc) · 2.37 KB
/
cachevector.R
File metadata and controls
62 lines (54 loc) · 2.37 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
## input x will be a vector
makeVector <- function(x = numeric()) {
## m will store our 'mean' and it's reset to NULL every
## time makeVector is called
## note these next three functions are not run when makeVector is called.
## instead, they will be used by cachemean() to get values for x or for
## m (mean) and for setting the mean. These are usually called
## object 'methods'.
m <- NULL
## takes an input vector
set <- function(y) {
## saves the input vector
x <<- y
## resets the mean to NULL, basically what happens when a new
## object is generated.
m <<- NULL
}
## this function returns the value of the original vector
get <- function() x
## this is called by cachemean() during the first cachemean()
## access and it will store the value using superassignment
setmean <- function(mean) m <<- mean
## this will return the cached value to cachemean() on
## subsequent accesses
getmean <- function() m
## This list is returned with the newly created object.
list(set = set, get = get,
## It lists all the functions ("methods") that are part of
setmean = setmean,
## the object. If a function is not on the list then it cannot
## be accessed externally.
getmean = getmean)
}
## the input is an object created by makeVector
cachemean <- function(x, ...) {
## accesses the object 'x' and gets the value of the mean
m <- x$getmean()
## if mean was already cached (not NULL) ...
if(!is.null(m)) {
## ... send this message to the console
message("getting cached data")
## ... and return the mean ... "return" ends
## the function cachemean(), note
return(m)
}
## we reach this code only if x$getmean() returned NULL
data <- x$get()
## if m was NULL then we have to calculate the mean
m <- mean(data, ...)
## store the calculated mean value in x (see setmean() in makeVector)
x$setmean(m)
## return the mean to the code that called this function
m
}