Jump to content

Closures/Value capture: Difference between revisions

Added Dyalect programming language
(Added Dyalect programming language)
Line 548:
→ 25
</lang>
 
=={{header|Dyalect}}==
Dyalect captures variables by reference, therefore a way to achieve this is to capture a variable through a closure which in its turn returns a anonymous function like so:
 
<lang dyalect>var xs = []
const num = 10
 
for n in 0..(num-1) {
var elem = x => () => x * x
xs.add(elem(n))
}
 
for x in xs {
print(x())
}</lang>
 
{{out}}
 
<pre>0
1
4
9
16
25
36
49
64
81</pre>
 
=={{header|Elena}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.