From f62253dd6c1b662ebb78b1b7e6aa09bd5fbea0f6 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 08:09:33 -0400 Subject: [PATCH 01/15] add easier to read TA remarks --- lexicalscoping.md | 197 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 lexicalscoping.md diff --git a/lexicalscoping.md b/lexicalscoping.md new file mode 100644 index 00000000000..ae0e8a1fb34 --- /dev/null +++ b/lexicalscoping.md @@ -0,0 +1,197 @@ +# Understanding Lexical Scoping in R – Great guidance by community TA in Coursera + +## Gregory D. HorneCommunity TA· 5 days ago + +I will show you the output from the sample functions to illustrate their behaviour. From the output you should be able to discern the logic within the R code. The functions we are expected to implement to handle invertible matrices follows the exact same logic. The sample run show below can be adapted to the new functions for matrices and the behaviour will be the same. + +```R +> a <- makeVector(c(1,2,3,4)) +> a$get() +[1] 1 2 3 4 +> a$getmean() +NULL +> cachemean(a) +[1] 2.5 +> a$getmean() # this is only to show you that the mean has been stored and does not affect anything +[1] 2.5 +> cachemean(a) +getting cached data +[1] 2.5 +> a$set(c(10,20,30,40)) +> a$getmean() +NULL +> cachemean(a) +[1] 25 +> cachemean(a) +getting cached data +[1] 25 +> a$get() +[1] 10 20 30 40 +> a$setmean(0) # do NOT call setmean() directly despite it being accessible for the reason you will see next +> a$getmean() +[1] 0 # obviously non-sense since… +> a$get() +[1] 10 20 30 40 +>cachemean(a) +[1] 0 # as you can see the call to setmean() effectively corrupted the functioning of the code +> a <- makeVector(c(5, 25, 125, 625)) +> a$get() +[1] 5 25 125 625 +> cachemean(a) +[1] 195 +> cachemean(a) +getting cached data +[1] 195 +``` + +Understanding the concepts in terms of the behaviour of the two functions takes more than a few minutes because you have to not only read the existing exemplar functions but take some time to play with the functions. Often seeing the code in action is more helpful than reading a chapter about lexical scoping. + +Variable m is declared uniquely in both functions and are allocated separate addresses in memory. In function makeVector() you’ll notice variable m is declared immediately and assigned the value NULL using the standard assignment operator (<-). However, the “set” functions defined within the containing makeVector() function require the special assignment operator (<–) to update the value of variable m; it is important to remember variable m was declared and initialised by makeVector(). + +Had functions set() and setmean() not used the special assignment operator, these functions would have allocated memory to store the value and labelled the address as m. The variables named m would effectively be isolated and distinct variables. + +## <- Operator Versus <<- Operator + +assignment operator: <- + +superassignment operator: <<- + +```R +crazy <- function() { + x <<- 3.14 # variable x in the containing environment (global in this case) is updated to be 3.14 + print(x) # since no local variable ‘x’ exists within function ‘crazy’ R searches the containing environments + { print(x); # this is to demonstrate the function, not a code block, is the smallest environment in R + x <- 42; print(x) # local variable ‘x’ is declared (created) and assigned the value 42; overrides the variable ‘x’ in + } # the containing environment + print(x) # since local variable ‘x’ now exists within the function there is no need to search the containing +} # environment (global in this case) +> x <- 0 +> crazy() +3.14 +3.14 +42 +42 +> x # variable ‘x’ outside of the function its updated value after the first statement within function ‘crazy()’ +[1] 3.14 +``` + +The first two print() statements use the variable ‘x’ in the containing environment, as no local variable ‘x’ exists at the moment, which has been updated from x <- 0 to x <- 3.14 via x <<- 3.14 inside function ‘crazy()’. + +The third print() statement uses the variable ‘x’ just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements. + +The fourth print() statement uses the variable ‘x’ which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable ‘x’. + +I added a call to variable ‘x’ after the function ‘crazy()’ returns to show it keeps the new value assigned to it by the super-assignment operator inside function ‘crazy()’. + +The super-assignment operator does not update a variable of the same name inside an inner function but the innermost environment inherits any changes unless a local variable of the same name exists within the inner function as demonstrated by x <- 42; print(x) and print(x). +Furthermore, if a variable named ‘x’ had existed inside function ‘crazy()’ and preceded the call to the super-assignment operator, the results would be as shown in the next example. + +```R +crazy <- function() { + x <- 42 + x <<- 3.14 + print(x) +} +> x <- 0 +> x +[1] 0 +> crazy() +42 +> x +[1] 3.14 +``` + +To reiterate the concept the next section explains in more detail the role and behaviour of the “superassignment” operator which allows the programmer to modify a variable declared outside of the current function in which the reference to the variable is made. +In the simplest example consider how variable ‘x’ changes when the crazy() function is called. + +```R +# Declare and define a function named crazy() +crazy <- function() { # create a new environment with a local variable ‘x’ and access to another variable ‘x’ + # declared somewhere outside this function + x <- 3.14 # assign the numeric value 3.14 to local variable ‘x’ + print(x) # output the current value of local variable ‘x’ (1) + { print(x); # output the current value of local variable ‘x’ (2) + x <<- 42; # assign the numeric value 42 to variable ‘x’ declared outside this function (3) + print(x) # output the current value of local variable ‘x’ (4) + } + print(x) # output the current value of local variable ‘x’ (5) +} +> x <- 0 # Declare and define a local variable named ‘x’ +> x # output the current value of local variable ‘x’ +0 +> crazy() # Call function crazy() +3.14 # (1) inner variable ‘x’ +3.14 # (2) inner variable ‘x’ +3.14 # (4) inner variable ‘x’ +3.14 # (5) inner variable ‘x’ +> x # (3) containing environment variable ‘x’ +42 +``` + +The curly braces (brackets) are intended to highlight the fact in R the smallest unit of lexical scoping is the function. Unlike some programming languages such as C that allow block-level variable scope, R treats a block enclosed in brackets as part of the nearest function. + +```R +x <- 3.14 +{ x <<- 42 } +``` +is treated as though it is as shown below. + +```R +x <- 3.24 # assigns the value 3.14 to local variable ‘x’ not the variable ‘x’ in the containing environment +x <<- 42 # assigns the value 42 to variable ‘x’ in the containing environment +``` + +Perhaps the crude graphic can illuminate the effects of lexical scoping on variables a better than mere words. +``` ++———————————+ +| (1a) x <- 0 becomes 42 | outer function / by default the global lexical scope is an anonymous function +| (1b) x <- 42 by | +| (3b) x <<- 42 | +| +—————————–+ | +| | (2) x <- 3.14 | | inner function +| | (3a) x <<- 42 does | | +| | not affect | | +| | (2) | | +| +—————————–+ | ++———————————+ +``` + +Flow of execution is (1a) -> [(2) & (3a)] -> [(3b) & (1b)] -> (1a). I used the labels “inner function” and “outer function” because the same rules apply to nested functions. + +## Unit tests (with expected output) for Programming Assignment 2 + +### Example +```R +> source(“cachematrix.R”) +> amatrix = makeCacheMatrix(matrix(c(1,2,3,4), nrow=2, ncol=2)) +> amatrix$get() # Returns original matrix + [,1] [,2] +[1,] 1 3 +[2,] 2 4 + > cacheSolve(amatrix) # Computes, caches, and returns matrix inverse + [,1] [,2] +[1,] -2 1.5 +[2,] 1 -0.5 + > amatrix$getinverse() # Returns matrix inverse + [,1] [,2] +[1,] -2 1.5 +[2,] 1 -0.5 + > cacheSolve(amatrix) # Returns cached matrix inverse using previously computed matrix inverse +getting cached data + [,1] [,2] +[1,] -2 1.5 +[2,] 1 -0.5 +> amatrix$set(matrix(c(0,5,99,66), nrow=2, ncol=2)) # Modify existing matrix +> cacheSolve(amatrix) # Computes, caches, and returns new matrix inverse + [,1] [,2] +[1,] -0.13333333 0.2 +[2,] 0.01010101 0.0 +> amatrix$get() # Returns matrix + [,1] [,2] +[1,] 0 99 +[2,] 5 66 +> amatrix$getinverse() # Returns matrix inverse + [,1] [,2] +[1,] -0.13333333 0.2 +[2,] 0.01010101 0.0 +``` \ No newline at end of file From 68b17803f7123742576be87bc69dd24874755c0f Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:30:57 -0400 Subject: [PATCH 02/15] markdown improvements --- lexicalscoping.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index ae0e8a1fb34..bf6cd1e6a71 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -1,6 +1,8 @@ # Understanding Lexical Scoping in R – Great guidance by community TA in Coursera -## Gregory D. HorneCommunity TA· 5 days ago +## Gregory D. Horne, Community TA + +(Note: This is easier to read version of: https://asitarrives.wordpress.com/2014/10/18/understanding-lexical-scoping-in-r-great-guidance-for-community-ta-in-coursera/) I will show you the output from the sample functions to illustrate their behaviour. From the output you should be able to discern the logic within the R code. The functions we are expected to implement to handle invertible matrices follows the exact same logic. The sample run show below can be adapted to the new functions for matrices and the behaviour will be the same. @@ -46,15 +48,15 @@ getting cached data Understanding the concepts in terms of the behaviour of the two functions takes more than a few minutes because you have to not only read the existing exemplar functions but take some time to play with the functions. Often seeing the code in action is more helpful than reading a chapter about lexical scoping. -Variable m is declared uniquely in both functions and are allocated separate addresses in memory. In function makeVector() you’ll notice variable m is declared immediately and assigned the value NULL using the standard assignment operator (<-). However, the “set” functions defined within the containing makeVector() function require the special assignment operator (<–) to update the value of variable m; it is important to remember variable m was declared and initialised by makeVector(). +Variable `m` is declared uniquely in both functions and are allocated separate addresses in memory. In function makeVector() you’ll notice variable `m` is declared immediately and assigned the value NULL using the standard assignment operator (`<-`). However, the `set` functions defined within the containing makeVector() function require the special assignment operator (`<<–`) to update the value of variable `m`; it is important to remember variable m was declared and initialised by makeVector(). -Had functions set() and setmean() not used the special assignment operator, these functions would have allocated memory to store the value and labelled the address as m. The variables named m would effectively be isolated and distinct variables. +Had functions `set()` and `setmean()` not used the special assignment operator, these functions would have allocated memory to store the value and labelled the address as `m`. The variables named `m` would effectively be isolated and distinct variables. ## <- Operator Versus <<- Operator -assignment operator: <- +assignment operator: `<-` -superassignment operator: <<- +superassignment operator: `<<-` ```R crazy <- function() { From 1c27bc076a12bfe2ffcbcd31685a363178d5850a Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:35:34 -0400 Subject: [PATCH 03/15] markdown improvements --- lexicalscoping.md | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index bf6cd1e6a71..cdb3e789848 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -48,7 +48,7 @@ getting cached data Understanding the concepts in terms of the behaviour of the two functions takes more than a few minutes because you have to not only read the existing exemplar functions but take some time to play with the functions. Often seeing the code in action is more helpful than reading a chapter about lexical scoping. -Variable `m` is declared uniquely in both functions and are allocated separate addresses in memory. In function makeVector() you’ll notice variable `m` is declared immediately and assigned the value NULL using the standard assignment operator (`<-`). However, the `set` functions defined within the containing makeVector() function require the special assignment operator (`<<–`) to update the value of variable `m`; it is important to remember variable m was declared and initialised by makeVector(). +Variable `m` is declared uniquely in both functions and are allocated separate addresses in memory. In function makeVector() you’ll notice variable `m` is declared immediately and assigned the value NULL using the standard assignment operator (`<-`). However, the `set` functions defined within the containing `makeVector()` function require the special assignment operator (`<<–`) to update the value of variable `m`; it is important to remember variable `m` was declared and initialised by `makeVector()`. Had functions `set()` and `setmean()` not used the special assignment operator, these functions would have allocated memory to store the value and labelled the address as `m`. The variables named `m` would effectively be isolated and distinct variables. @@ -60,13 +60,13 @@ superassignment operator: `<<-` ```R crazy <- function() { - x <<- 3.14 # variable x in the containing environment (global in this case) is updated to be 3.14 - print(x) # since no local variable ‘x’ exists within function ‘crazy’ R searches the containing environments - { print(x); # this is to demonstrate the function, not a code block, is the smallest environment in R - x <- 42; print(x) # local variable ‘x’ is declared (created) and assigned the value 42; overrides the variable ‘x’ in - } # the containing environment - print(x) # since local variable ‘x’ now exists within the function there is no need to search the containing -} # environment (global in this case) + x <<- 3.14 # variable x in the containing environment (global in this case) is updated to be 3.14 + print(x) # since no local variable ‘x’ exists within function ‘crazy’ R searches the containing environments + { print(x); # this is to demonstrate the function, not a code block, is the smallest environment in R + x <- 42; print(x) # local variable ‘x’ is declared (created) and assigned the value 42; overrides the variable ‘x’ in + } # the containing environment + print(x) # since local variable ‘x’ now exists within the function there is no need to search the containing +} # environment (global in this case) > x <- 0 > crazy() 3.14 @@ -108,25 +108,25 @@ In the simplest example consider how variable ‘x’ changes when the crazy() f ```R # Declare and define a function named crazy() -crazy <- function() { # create a new environment with a local variable ‘x’ and access to another variable ‘x’ - # declared somewhere outside this function - x <- 3.14 # assign the numeric value 3.14 to local variable ‘x’ - print(x) # output the current value of local variable ‘x’ (1) - { print(x); # output the current value of local variable ‘x’ (2) - x <<- 42; # assign the numeric value 42 to variable ‘x’ declared outside this function (3) - print(x) # output the current value of local variable ‘x’ (4) +crazy <- function() { # create a new environment with a local variable ‘x’ and access to another variable ‘x’ + # declared somewhere outside this function + x <- 3.14 # assign the numeric value 3.14 to local variable ‘x’ + print(x) # output the current value of local variable ‘x’ (1) + { print(x); # output the current value of local variable ‘x’ (2) + x <<- 42; # assign the numeric value 42 to variable ‘x’ declared outside this function (3) + print(x) # output the current value of local variable ‘x’ (4) } - print(x) # output the current value of local variable ‘x’ (5) + print(x) # output the current value of local variable ‘x’ (5) } -> x <- 0 # Declare and define a local variable named ‘x’ -> x # output the current value of local variable ‘x’ +> x <- 0 # Declare and define a local variable named ‘x’ +> x # output the current value of local variable ‘x’ 0 -> crazy() # Call function crazy() -3.14 # (1) inner variable ‘x’ -3.14 # (2) inner variable ‘x’ -3.14 # (4) inner variable ‘x’ -3.14 # (5) inner variable ‘x’ -> x # (3) containing environment variable ‘x’ +> crazy() # Call function crazy() +3.14 # (1) inner variable ‘x’ +3.14 # (2) inner variable ‘x’ +3.14 # (4) inner variable ‘x’ +3.14 # (5) inner variable ‘x’ +> x # (3) containing environment variable ‘x’ 42 ``` @@ -139,8 +139,8 @@ x <- 3.14 is treated as though it is as shown below. ```R -x <- 3.24 # assigns the value 3.14 to local variable ‘x’ not the variable ‘x’ in the containing environment -x <<- 42 # assigns the value 42 to variable ‘x’ in the containing environment +x <- 3.24 # assigns the value 3.14 to local variable ‘x’ not the variable ‘x’ in the containing environment +x <<- 42 # assigns the value 42 to variable ‘x’ in the containing environment ``` Perhaps the crude graphic can illuminate the effects of lexical scoping on variables a better than mere words. @@ -174,11 +174,11 @@ Flow of execution is (1a) -> [(2) & (3a)] -> [(3b) & (1b)] -> (1a). I used the l [,1] [,2] [1,] -2 1.5 [2,] 1 -0.5 - > amatrix$getinverse() # Returns matrix inverse + > amatrix$getinverse() # Returns matrix inverse [,1] [,2] [1,] -2 1.5 [2,] 1 -0.5 - > cacheSolve(amatrix) # Returns cached matrix inverse using previously computed matrix inverse + > cacheSolve(amatrix) # Returns cached matrix inverse using previously computed matrix inverse getting cached data [,1] [,2] [1,] -2 1.5 From 76e34d1ba5863f17cff3ede003d93fd91aac4887 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:36:44 -0400 Subject: [PATCH 04/15] markdown improvements --- lexicalscoping.md | 52 +++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index cdb3e789848..3f5d0d1a794 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -61,11 +61,11 @@ superassignment operator: `<<-` ```R crazy <- function() { x <<- 3.14 # variable x in the containing environment (global in this case) is updated to be 3.14 - print(x) # since no local variable ‘x’ exists within function ‘crazy’ R searches the containing environments + print(x) # since no local variable `x` exists within function ‘crazy’ R searches the containing environments { print(x); # this is to demonstrate the function, not a code block, is the smallest environment in R - x <- 42; print(x) # local variable ‘x’ is declared (created) and assigned the value 42; overrides the variable ‘x’ in + x <- 42; print(x) # local variable `x` is declared (created) and assigned the value 42; overrides the variable `x` in } # the containing environment - print(x) # since local variable ‘x’ now exists within the function there is no need to search the containing + print(x) # since local variable `x` now exists within the function there is no need to search the containing } # environment (global in this case) > x <- 0 > crazy() @@ -73,20 +73,20 @@ crazy <- function() { 3.14 42 42 -> x # variable ‘x’ outside of the function its updated value after the first statement within function ‘crazy()’ +> x # variable `x` outside of the function its updated value after the first statement within function ‘crazy()’ [1] 3.14 ``` -The first two print() statements use the variable ‘x’ in the containing environment, as no local variable ‘x’ exists at the moment, which has been updated from x <- 0 to x <- 3.14 via x <<- 3.14 inside function ‘crazy()’. +The first two print() statements use the variable `x` in the containing environment, as no local variable `x` exists at the moment, which has been updated from x <- 0 to x <- 3.14 via x <<- 3.14 inside function ‘crazy()’. -The third print() statement uses the variable ‘x’ just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements. +The third print() statement uses the variable `x` just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements. -The fourth print() statement uses the variable ‘x’ which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable ‘x’. +The fourth print() statement uses the variable `x` which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable `x`. -I added a call to variable ‘x’ after the function ‘crazy()’ returns to show it keeps the new value assigned to it by the super-assignment operator inside function ‘crazy()’. +I added a call to variable `x` after the function ‘crazy()’ returns to show it keeps the new value assigned to it by the super-assignment operator inside function ‘crazy()’. The super-assignment operator does not update a variable of the same name inside an inner function but the innermost environment inherits any changes unless a local variable of the same name exists within the inner function as demonstrated by x <- 42; print(x) and print(x). -Furthermore, if a variable named ‘x’ had existed inside function ‘crazy()’ and preceded the call to the super-assignment operator, the results would be as shown in the next example. +Furthermore, if a variable named `x` had existed inside function ‘crazy()’ and preceded the call to the super-assignment operator, the results would be as shown in the next example. ```R crazy <- function() { @@ -104,29 +104,29 @@ crazy <- function() { ``` To reiterate the concept the next section explains in more detail the role and behaviour of the “superassignment” operator which allows the programmer to modify a variable declared outside of the current function in which the reference to the variable is made. -In the simplest example consider how variable ‘x’ changes when the crazy() function is called. +In the simplest example consider how variable `x` changes when the crazy() function is called. ```R # Declare and define a function named crazy() -crazy <- function() { # create a new environment with a local variable ‘x’ and access to another variable ‘x’ +crazy <- function() { # create a new environment with a local variable `x` and access to another variable `x` # declared somewhere outside this function - x <- 3.14 # assign the numeric value 3.14 to local variable ‘x’ - print(x) # output the current value of local variable ‘x’ (1) - { print(x); # output the current value of local variable ‘x’ (2) - x <<- 42; # assign the numeric value 42 to variable ‘x’ declared outside this function (3) - print(x) # output the current value of local variable ‘x’ (4) + x <- 3.14 # assign the numeric value 3.14 to local variable `x` + print(x) # output the current value of local variable `x` (1) + { print(x); # output the current value of local variable `x` (2) + x <<- 42; # assign the numeric value 42 to variable `x` declared outside this function (3) + print(x) # output the current value of local variable `x` (4) } - print(x) # output the current value of local variable ‘x’ (5) + print(x) # output the current value of local variable `x` (5) } -> x <- 0 # Declare and define a local variable named ‘x’ -> x # output the current value of local variable ‘x’ +> x <- 0 # Declare and define a local variable named `x` +> x # output the current value of local variable `x` 0 > crazy() # Call function crazy() -3.14 # (1) inner variable ‘x’ -3.14 # (2) inner variable ‘x’ -3.14 # (4) inner variable ‘x’ -3.14 # (5) inner variable ‘x’ -> x # (3) containing environment variable ‘x’ +3.14 # (1) inner variable `x` +3.14 # (2) inner variable `x` +3.14 # (4) inner variable `x` +3.14 # (5) inner variable `x` +> x # (3) containing environment variable `x` 42 ``` @@ -139,8 +139,8 @@ x <- 3.14 is treated as though it is as shown below. ```R -x <- 3.24 # assigns the value 3.14 to local variable ‘x’ not the variable ‘x’ in the containing environment -x <<- 42 # assigns the value 42 to variable ‘x’ in the containing environment +x <- 3.24 # assigns the value 3.14 to local variable `x` not the variable `x` in the containing environment +x <<- 42 # assigns the value 42 to variable `x` in the containing environment ``` Perhaps the crude graphic can illuminate the effects of lexical scoping on variables a better than mere words. From d451bed1b7b49929c0764f8492b97588f9e188f5 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:39:34 -0400 Subject: [PATCH 05/15] markdown improvements --- lexicalscoping.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index 3f5d0d1a794..e6ff0522752 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -73,20 +73,20 @@ crazy <- function() { 3.14 42 42 -> x # variable `x` outside of the function its updated value after the first statement within function ‘crazy()’ +> x # variable `x` outside of the function its updated value after the first statement within function `crazy()` [1] 3.14 ``` -The first two print() statements use the variable `x` in the containing environment, as no local variable `x` exists at the moment, which has been updated from x <- 0 to x <- 3.14 via x <<- 3.14 inside function ‘crazy()’. +The first two `print()` statements use the variable `x` in the containing environment, as no local variable `x` exists at the moment, which has been updated from `x <- 0` to `x <- 3.14` via `x <<- 3.14` inside function `crazy()`. The third print() statement uses the variable `x` just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements. The fourth print() statement uses the variable `x` which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable `x`. -I added a call to variable `x` after the function ‘crazy()’ returns to show it keeps the new value assigned to it by the super-assignment operator inside function ‘crazy()’. +I added a call to variable `x` after the function `crazy()` returns to show it keeps the new value assigned to it by the super-assignment operator inside function `crazy()`. The super-assignment operator does not update a variable of the same name inside an inner function but the innermost environment inherits any changes unless a local variable of the same name exists within the inner function as demonstrated by x <- 42; print(x) and print(x). -Furthermore, if a variable named `x` had existed inside function ‘crazy()’ and preceded the call to the super-assignment operator, the results would be as shown in the next example. +Furthermore, if a variable named `x` had existed inside function `crazy()` and preceded the call to the super-assignment operator, the results would be as shown in the next example. ```R crazy <- function() { From 7346d15aef3291017991beef0799ddc1a611e8d8 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:40:11 -0400 Subject: [PATCH 06/15] markdown improvements --- lexicalscoping.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index e6ff0522752..21b79a59528 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -79,9 +79,9 @@ crazy <- function() { The first two `print()` statements use the variable `x` in the containing environment, as no local variable `x` exists at the moment, which has been updated from `x <- 0` to `x <- 3.14` via `x <<- 3.14` inside function `crazy()`. -The third print() statement uses the variable `x` just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements. +The third `print()` statement uses the variable `x` just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second `print()` statements. -The fourth print() statement uses the variable `x` which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable `x`. +The fourth `print()` statement uses the variable `x` which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable `x`. I added a call to variable `x` after the function `crazy()` returns to show it keeps the new value assigned to it by the super-assignment operator inside function `crazy()`. From 276f872fb35d0d05de32c1694be96f354a7b473f Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:40:55 -0400 Subject: [PATCH 07/15] markdown improvements --- lexicalscoping.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index 21b79a59528..8095523503f 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -79,9 +79,9 @@ crazy <- function() { The first two `print()` statements use the variable `x` in the containing environment, as no local variable `x` exists at the moment, which has been updated from `x <- 0` to `x <- 3.14` via `x <<- 3.14` inside function `crazy()`. -The third `print()` statement uses the variable `x` just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second `print()` statements. +The third `print()` statement uses the variable `x` just created by the preceding assignment statement `x <- 42` which causes the containing environment not to be searched unlike the first and second `print()` statements. -The fourth `print()` statement uses the variable `x` which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable `x`. +The fourth `print()` statement uses the variable `x` which exists within the function because the `x <- 42` now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable `x`. I added a call to variable `x` after the function `crazy()` returns to show it keeps the new value assigned to it by the super-assignment operator inside function `crazy()`. From fb1c3beaea2eaac8a9ca505d95e08ac5f210f88b Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:41:29 -0400 Subject: [PATCH 08/15] markdown improvements --- lexicalscoping.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index 8095523503f..e936c11e91e 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -85,7 +85,7 @@ The fourth `print()` statement uses the variable `x` which exists within the fun I added a call to variable `x` after the function `crazy()` returns to show it keeps the new value assigned to it by the super-assignment operator inside function `crazy()`. -The super-assignment operator does not update a variable of the same name inside an inner function but the innermost environment inherits any changes unless a local variable of the same name exists within the inner function as demonstrated by x <- 42; print(x) and print(x). +The super-assignment operator does not update a variable of the same name inside an inner function but the innermost environment inherits any changes unless a local variable of the same name exists within the inner function as demonstrated by `x <- 42`; `print(x)` and `print(x)`. Furthermore, if a variable named `x` had existed inside function `crazy()` and preceded the call to the super-assignment operator, the results would be as shown in the next example. ```R From c338706db02a73685e10d9d3a4fe071969fc6006 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:42:38 -0400 Subject: [PATCH 09/15] markdown improvements --- lexicalscoping.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index e936c11e91e..729d8697076 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -104,7 +104,7 @@ crazy <- function() { ``` To reiterate the concept the next section explains in more detail the role and behaviour of the “superassignment” operator which allows the programmer to modify a variable declared outside of the current function in which the reference to the variable is made. -In the simplest example consider how variable `x` changes when the crazy() function is called. +In the simplest example consider how variable `x` changes when the `crazy()` function is called. ```R # Declare and define a function named crazy() From db6a2cb0b2293a25e9cb58556ec9e6716b4f7648 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:43:41 -0400 Subject: [PATCH 10/15] markdown improvements --- lexicalscoping.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index 729d8697076..17afe03eece 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -130,7 +130,7 @@ crazy <- function() { # create a new environment with a local variable `x` an 42 ``` -The curly braces (brackets) are intended to highlight the fact in R the smallest unit of lexical scoping is the function. Unlike some programming languages such as C that allow block-level variable scope, R treats a block enclosed in brackets as part of the nearest function. +*The curly braces (brackets) are intended to highlight the fact in R the smallest unit of lexical scoping is the function.* Unlike some programming languages such as C that allow block-level variable scope, R treats a block enclosed in brackets as part of the nearest function. ```R x <- 3.14 From 2951e88f54b24edfa482900f19abbf6ff7015ac3 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:44:00 -0400 Subject: [PATCH 11/15] markdown improvements --- lexicalscoping.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index 17afe03eece..81eb3c1f6e2 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -130,7 +130,7 @@ crazy <- function() { # create a new environment with a local variable `x` an 42 ``` -*The curly braces (brackets) are intended to highlight the fact in R the smallest unit of lexical scoping is the function.* Unlike some programming languages such as C that allow block-level variable scope, R treats a block enclosed in brackets as part of the nearest function. +**The curly braces (brackets) are intended to highlight the fact in R the smallest unit of lexical scoping is the function.** Unlike some programming languages such as C that allow block-level variable scope, R treats a block enclosed in brackets as part of the nearest function. ```R x <- 3.14 From 550039e2f7958faf950dcc4b7a554ebf72bb0447 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 11:49:12 -0400 Subject: [PATCH 12/15] markdown improvements --- lexicalscoping.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index 81eb3c1f6e2..6224ea999ba 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -146,19 +146,22 @@ x <<- 42 # assigns the value 42 to variable `x` in the containing environme Perhaps the crude graphic can illuminate the effects of lexical scoping on variables a better than mere words. ``` +———————————+ -| (1a) x <- 0 becomes 42 | outer function / by default the global lexical scope is an anonymous function -| (1b) x <- 42 by | -| (3b) x <<- 42 | -| +—————————–+ | -| | (2) x <- 3.14 | | inner function -| | (3a) x <<- 42 does | | -| | not affect | | -| | (2) | | -| +—————————–+ | +| (1a) x <- 0 becomes 42 +| outer function / by default the global lexical scope is an anonymous function +| (1b) x <- 42 by +| +| (3b) x <<- 42 +| +| +—————————–+ +| | (2) x <- 3.14 +| | inner function +| | (3a) x <<- 42 does not affect (2) +| | +| +—————————–+ +———————————+ ``` -Flow of execution is (1a) -> [(2) & (3a)] -> [(3b) & (1b)] -> (1a). I used the labels “inner function” and “outer function” because the same rules apply to nested functions. +Flow of execution is `(1a) -> [(2) & (3a)] -> [(3b) & (1b)] -> (1a)`. I used the labels “inner function” and “outer function” because the same rules apply to nested functions. ## Unit tests (with expected output) for Programming Assignment 2 From 3ee94a79415e68f92c712c63d5b86b7abce48e52 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 13:15:48 -0400 Subject: [PATCH 13/15] markdown improvements --- lexicalscoping.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lexicalscoping.md b/lexicalscoping.md index 6224ea999ba..a1160e60d4e 100644 --- a/lexicalscoping.md +++ b/lexicalscoping.md @@ -103,7 +103,7 @@ crazy <- function() { [1] 3.14 ``` -To reiterate the concept the next section explains in more detail the role and behaviour of the “superassignment” operator which allows the programmer to modify a variable declared outside of the current function in which the reference to the variable is made. +To reiterate the concept the next section explains in more detail the role and behaviour of the "superassignment" operator which allows the programmer to modify a variable declared outside of the current function in which the reference to the variable is made. In the simplest example consider how variable `x` changes when the `crazy()` function is called. ```R @@ -161,13 +161,13 @@ Perhaps the crude graphic can illuminate the effects of lexical scoping on varia +———————————+ ``` -Flow of execution is `(1a) -> [(2) & (3a)] -> [(3b) & (1b)] -> (1a)`. I used the labels “inner function” and “outer function” because the same rules apply to nested functions. +Flow of execution is `(1a) -> [(2) & (3a)] -> [(3b) & (1b)] -> (1a)`. I used the labels "inner function" and "outer function" because the same rules apply to nested functions. ## Unit tests (with expected output) for Programming Assignment 2 ### Example ```R -> source(“cachematrix.R”) +> source("cachematrix.R") > amatrix = makeCacheMatrix(matrix(c(1,2,3,4), nrow=2, ncol=2)) > amatrix$get() # Returns original matrix [,1] [,2] From db6d076c7c5f25f3e7cc6fa2e39be5a6ac220bf9 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 13:25:42 -0400 Subject: [PATCH 14/15] Working inverse matrixfunctions and cache --- cachematrix.R | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..5774345c91f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,46 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + # first make a local variable `im` for the inverse matrx + im <- NULL + # enable setting of global env variables + set <- function(y) { + ## scope matrix to a global env variable `x` + x <<- y + ## scope an empty global env variable `im` + im <<- NULL + } + ## return original matrix + get <- function() {x} + ## Set `im` to be calculated inversematrix in global environment lexical scope by using `<<-` + setinverse <- function(inversematrix) { + im <<- inversematrix + } + # retrieve the inverse matrix of x + getinverse <- function() {im} + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) } ## Write a short comment describing this function +## assume matrix `x` is always invertable cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + ## Return a matrix that is the inverse of 'x' + # `x` is a matrix + # `ix` is inverse of matrix + + # `x` is makeCacheMatrix object + ix <- x$getinverse() + if (!is.null(ix)) { + message("getting cached data") + return(ix) + } + + ## inverse the matrix + message("calculating inverse and caching result") + matrix <- x$get() + ix <- solve(matrix, ...) + x$setinverse(ix) + ix +} \ No newline at end of file From 3ce300d92702e9d3e18e5a5c954f632fe3fcea04 Mon Sep 17 00:00:00 2001 From: Greg Elin Date: Sun, 25 Oct 2015 13:33:49 -0400 Subject: [PATCH 15/15] improve comments --- cachematrix.R | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 5774345c91f..ae8e4060015 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,8 +1,6 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## This pair of functions will calculate and cache the inverse of an invertible matrix +## `makeCacheMatrix` accepts an invertible matrix and contains methods to cache inverse value makeCacheMatrix <- function(x = matrix()) { # first make a local variable `im` for the inverse matrx im <- NULL @@ -25,9 +23,8 @@ makeCacheMatrix <- function(x = matrix()) { } -## Write a short comment describing this function -## assume matrix `x` is always invertable - +## `cacheSolve` accepts an makeCacheMatrix and caclulates the inverted matrix +## or retrieves the cached matrix cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' # `x` is a matrix