Skip to content

Commit ce1049e

Browse files
committed
cacheSolve added
1 parent 8c6cc3f commit ce1049e

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

cachematrix.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,20 @@ makeCacheMatrix <- function(local_matrix = matrix()) {
3434
get_solved = get_solved)
3535
}
3636

37-
## Write a short comment describing this function
38-
39-
cacheSolve <- function(x, ...) {
40-
## Return a matrix that is the inverse of 'x'
37+
## Consumer of makeCacheMatrix
38+
cacheSolve <- function(cache_matrix, ...) {
39+
# Get "solved" matrix
40+
# If solution isn't cached it will be null
41+
solution <- cache_matrix$get_solved()
42+
if(!is.null(solution)) {
43+
# If solution is cached it will be returned
44+
message("Getting cached matrix")
45+
return(solution)
46+
} else {
47+
# Solve it...
48+
solution <- solve(cache_matrix$get_matrix(), ...)
49+
# Put in cache just before return
50+
cache_matrix$set_solved(solution)
51+
return(solution)
52+
}
4153
}

0 commit comments

Comments
 (0)