Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ceee71b

Browse files
committed
minor refactor
1 parent dda193a commit ceee71b

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

cachematrix.R

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,21 @@ makeCacheMatrix <- function(dataMatrix = matrix()) {
2727
## within a wrapper list created by the makeCacheMatrix function.
2828

2929
cacheSolve <- function(matrixWrapper, ...) {
30-
3130
#Retrieve the inverse stored in the wrapper object
3231
inverse = matrixWrapper$getInverse()
3332

3433
if (!is.null(inverse)) {
3534
#Return the pre-calculated inverse
3635
message("getting cached data")
37-
return(inverse)
36+
} else {
37+
# No inverse was found, so calculate it
38+
message("calculating data")
39+
dataMatrix = matrixWrapper$get()
40+
inverse = solve(dataMatrix)
41+
42+
# Store the caluclated inverse for future use
43+
matrixWrapper$setInverse(inverse)
3844
}
3945

40-
# No inverse was found, so calculate it
41-
message("calculating data")
42-
dataMatrix = matrixWrapper$get()
43-
inverse = solve(dataMatrix)
44-
45-
# Store the caluclated inverse for future use
46-
matrixWrapper$setInverse(inverse)
47-
4846
inverse
4947
}

0 commit comments

Comments
 (0)