Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Closures-Value-capture/R/closures-value-capture-1.r
Normal file
9
Task/Closures-Value-capture/R/closures-value-capture-1.r
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# assign 's' a list of ten functions
|
||||
s <- sapply (1:10, # integers 1..10 become argument 'x' below
|
||||
function (x) {
|
||||
x # force evaluation of promise x
|
||||
function (i=x) i*i # this *function* is the return value
|
||||
})
|
||||
|
||||
s[[5]]() # call the fifth function in the list of returned functions
|
||||
[1] 25 # returns vector of length 1 with the value 25
|
||||
2
Task/Closures-Value-capture/R/closures-value-capture-2.r
Normal file
2
Task/Closures-Value-capture/R/closures-value-capture-2.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
s[[5]](10)
|
||||
[1] 100
|
||||
18
Task/Closures-Value-capture/R/closures-value-capture-3.r
Normal file
18
Task/Closures-Value-capture/R/closures-value-capture-3.r
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
s <- sapply (1:10,
|
||||
function (x) {
|
||||
x # force evaluation of promise x
|
||||
function () {
|
||||
R <- x*x
|
||||
# evaluate the language expression "x <- x + 1" in the persistent parent environment
|
||||
evalq (x <- x + 1, parent.env(environment()))
|
||||
R # return squared value
|
||||
}})
|
||||
|
||||
s[[5]]()
|
||||
[1] 25 # 5^2
|
||||
s[[5]]()
|
||||
[1] 36 # now 6^2
|
||||
s[[1]]()
|
||||
[1] 1 # 1^2
|
||||
s[[1]]()
|
||||
[1] 4 # now 2^2
|
||||
1
Task/Closures-Value-capture/R/closures-value-capture-4.r
Normal file
1
Task/Closures-Value-capture/R/closures-value-capture-4.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
evalq (x <- x + 1, parent.env(environment()))
|
||||
1
Task/Closures-Value-capture/R/closures-value-capture-5.r
Normal file
1
Task/Closures-Value-capture/R/closures-value-capture-5.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
x <<- x + 1
|
||||
Loading…
Add table
Add a link
Reference in a new issue