@@ -20,7 +20,7 @@ makeCacheMatrix <- function(x = matrix()) {
20
20
21
21
# update matrix value
22
22
set <- function (newMatrix , inverse ) {
23
- stopifnot_square (newMatrix )
23
+ ensure_square_matrix (newMatrix )
24
24
x <<- newMatrix
25
25
inverse <<- NULL # new matrix invalidates old cache value
26
26
}
@@ -29,7 +29,7 @@ makeCacheMatrix <- function(x = matrix()) {
29
29
30
30
# hard set inverse to X
31
31
setInverse <- function (x ) {
32
- stopifnot_square (x )
32
+ ensure_square_matrix (x )
33
33
inverse <<- x
34
34
}
35
35
@@ -48,13 +48,13 @@ makeCacheMatrix <- function(x = matrix()) {
48
48
cacheSolve <- function (x , ... ) {
49
49
50
50
# # Basic sanity checks for input
51
- stopifnot(! is.null(x ))
52
- stopifnot(! is.na(x ))
53
- stopifnot(! is.matrix(x ))
54
- stopifnot_square(x )
51
+ ensure(is.null(x ), ' Input is null' )
52
+ ensure(is.na(x ), ' Input value is missing' )
53
+ ensure(is.matrix(x ), ' Input is not a matrix' )
54
+ ensure_square_matrix(x )
55
+
55
56
# # Try get result from cache, return result if found
56
57
inverse <- x $ getInverse()
57
-
58
58
if (! is.null(inverse )) {
59
59
print(' Return inverse from cache' )
60
60
return (inverse )
@@ -69,9 +69,13 @@ cacheSolve <- function(x, ...) {
69
69
inverse
70
70
}
71
71
72
- stopifnot_square <- function (x ) {
72
+ ensure_square_matrix <- function (x ) {
73
73
d <- dim(x )
74
- ifelse((d [1 ] == d [2 ]), TRUE , stop(' Not a square matrix' ))
74
+ ensure(! (d [1 ] == d [2 ]), ' Not a square matrix' )
75
+ }
76
+
77
+ ensure <- function (cond , message ) {
78
+ ifelse(cond , stop(message ), TRUE )
75
79
}
76
80
77
81
# Basic tests for validation
0 commit comments