FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ProgrammingAssignment2/functionTests.R at master · pputz/ProgrammingAssignment2 · GitHub
pputz
/
ProgrammingAssignment2
Public
forked from
rdpeng/ProgrammingAssignment2
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
ProgrammingAssignment2
/
functionTests.R
Copy path
More file actions
More file actions
Latest commit
History
History
History
108 lines (66 loc) · 1.93 KB
Breadcrumbs
ProgrammingAssignment2
/
functionTests.R
Copy path
File metadata and controls
108 lines (66 loc) · 1.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
###############################################################
#
Test caching for matrix inverse
#
setwd("")
rm(
list
=
ls())
source(
"
cachematrix.R
"
)
#
mx <- matrix(1:4, 2, 2)
mx
<-
matrix
(sample(
10
,
100
,
replace
=
TRUE
),
10
,
10
)
mx
mx.1
<-
makeCacheMatrix(
mx
)
mx.inv
<-
cacheSolve(
mx.1
)
mx.inv
round((
mx
%*%
mx.inv
),
3
)
mx1
ls(environment(
mx.1
$
getinverse
))
get(
"
x
"
, environment(
mx.1
$
getinverse
))
get(
"
inv
"
, environment(
mx.1
$
getinverse
))
#
#################################################################
#
Test caching for vector mean
rm(
list
=
ls())
source(
"
cachevector.R
"
)
a
<-
makeVector(
1
:
30
)
a
environment(
a
)
environment(
a
$
set
)
ls(environment(
a
$
set
))
get(
"
x
"
, environment(
a
$
get
))
get(
"
m
"
, environment(
a
$
get
))
cachemean(
a
)
ls(environment(
a
$
set
))
get(
"
x
"
, environment(
a
$
get
))
get(
"
m
"
, environment(
a
$
get
))
#
#################################################################
#
Another caching example: counter
createCounter
<-
function
(
x
) {
function
(
i
) {
value
<<-
value
+
i
} }
counter
<-
createCounter(
2
)
counter
ls(environment(
counter
))
get(
"
value
"
, environment(
counter
))
#
This SEEMS todemonstrates that 2 is
#
- passed on to "value" in the outer function and
#
- then passed to "i" in the inner function.
#
At the end 2 gets stored in the environment of
#
the inner funtion as "value"
#
#
HOWEVER: Let's see what happens when I replace
#
"x" with "increm"
rm(
list
=
ls())
createCounter
<-
function
(
increm
) {
function
(
i
) {
value
<<-
value
+
i
} }
counter
<-
createCounter(
3
)
counter
ls(environment(
counter
))
get(
"
increm
"
, environment(
counter
))
#
The only variable in the environment of "counter"
#
is "increm" and NOT "value".
#
This shows that function(i) was not even executed.
#
Instead the outer function "automatically" saved the
#
variable "increm" and assigned 3 to it.
a
<-
counter(
0
)
a
get(
"
value
"
, environment(
counter
))
counter(
3
)
a
get(
"
value
"
, environment(
counter
))
a
<-
counter(
0
)
a
get(
"
value
"
, environment(
counter
))
Back
|
FazBrowse Home
|
New Git URL