4
4
# mci <- makeCacheMatrix({message("calculating");solve(m)})
5
5
# cacheSolve(mci)
6
6
# cacheSolve(mci) # should not print message again, should insted inform about cache use
7
- # mcs <- makeCacheMatrix(solve(m, c(1,1))
7
+ # mcs <- makeCacheMatrix(m, c(1,1))
8
8
# cacheSolve(mcs)
9
9
10
10
# Class for creating matrices with
11
11
# ability to cache results of operations
12
- # see CacheOps$help(getResult)
13
12
CachedOps <- setRefClass(" CachedOps" ,
14
- fields = list (result = " function" , cached = " logical " ),
13
+ fields = list ( func = " function" , cache = " ANY " ),
15
14
16
15
methods = list (
17
- initialize = function (expr ,... ){
18
- cached <<- FALSE
19
- result <<- function (){
20
- cached <<- TRUE
21
- expr
22
- }
23
- callSuper(... )
16
+ initialize = function (... ,func ){
17
+ cache <<- NULL
18
+ func <<- function (){
19
+ func(... )
20
+ }
21
+ callSuper()
24
22
},
25
23
26
24
getResult = function (){
@@ -31,17 +29,17 @@ Once it was calculated result is cached and returned in any consecutive call"
31
29
})
32
30
)
33
31
34
- makeCacheMatrix <- function (expr , ... ) {
35
- # Creating new caching object
36
- # Args:
37
- # func - function, result of calculation is need to cache
38
- # ... - parameters for function calling
39
- CachedOps $ new(expr , ... )
32
+ makeCacheMatrix <- function (... , func = solve ) {
33
+ # Creating new caching object
34
+ # Args:
35
+ # func - function, result of calculation is need to cache
36
+ # ... - parameters for function calling
37
+ CachedOps $ new(... , func = func )
40
38
}
41
39
42
- cacheSolve <- function (x ) {
43
- # Calculating result of function on matrix
44
- # once it was calculated result is cached
45
- # and returned in any consecutive call
40
+ cacheSolve <- function (x , ... ) {
41
+ # Calculating result of function on matrix
42
+ # once it was calculated result is cached
43
+ # and returned in any consecutive call
46
44
x $ getResult()
47
45
}
0 commit comments