Jump to content

Closures/Value capture: Difference between revisions

Line 1,645:
 
As shown, each instance increments separately.
 
 
<b>--- Edit ---</b>
 
I think that modifying the bound variable can be done in a simpler way.
Instead of:
<lang R> evalq (x <- x + 1, parent.env(environment()))</lang>
substitute:
<lang R> x <<- x + 1</lang>
Testing:
<pre>
> s[[5]]()
[1] 25
> s[[5]]()
[1] 36
> s[[5]]()
[1] 49
> s[[2]]()
[1] 4
> s[[2]]()
[1] 9
> s[[2]]()
[1] 16
</pre>
 
=={{header|Racket}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.