Closures/Value capture: Difference between revisions

Content added Content deleted
Line 1,645: Line 1,645:


As shown, each instance increments separately.
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}}==
=={{header|Racket}}==