Jump to content

Closures/Value capture: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Swift}}: + standard ML)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 341:
1
0
</pre>
 
=={{header|C++}}==
{{works with|C++11}}
<lang cpp>#include <iostream>
#include <functional>
#include <vector>
 
int main() {
std::vector<std::function<int()> > funcs;
for (int i = 0; i < 10; i++)
funcs.push_back([=]() { return i * i; });
for ( std::function<int( )> f : funcs )
std::cout << f( ) << std::endl ;
return 0;
}</lang>
{{out}}
<pre>0
1
4
9
16
25
36
49
64
81
</pre>
 
Line 434 ⟶ 407:
49
64</lang>
 
=={{header|C++}}==
{{works with|C++11}}
<lang cpp>#include <iostream>
#include <functional>
#include <vector>
 
int main() {
std::vector<std::function<int()> > funcs;
for (int i = 0; i < 10; i++)
funcs.push_back([=]() { return i * i; });
for ( std::function<int( )> f : funcs )
std::cout << f( ) << std::endl ;
return 0;
}</lang>
{{out}}
<pre>0
1
4
9
16
25
36
49
64
81
</pre>
 
=={{header|Ceylon}}==
Line 540:
81
</pre>
 
=={{header|EchoLisp}}==
<lang scheme>
(define (fgen i) (lambda () (* i i)))
(define fs (for/vector ((i 10)) (fgen i))) ;; vector of 10 anonymous functions
((vector-ref fs 5)) ;; calls fs[5]
→ 25
</lang>
 
=={{header|Dyalect}}==
Line 577 ⟶ 569:
 
This is similar to a JavaScript (ES6) solution.
 
=={{header|EchoLisp}}==
<lang scheme>
(define (fgen i) (lambda () (* i i)))
(define fs (for/vector ((i 10)) (fgen i))) ;; vector of 10 anonymous functions
((vector-ref fs 5)) ;; calls fs[5]
→ 25
</lang>
 
=={{header|Elena}}==
Line 660:
100
ok
</pre>
 
=={{header|FreeBASIC}}==
 
FreeBASIC doesn't support closures or anonymous methods, as such. However, what we can do is to create an array of objects to capture their index and then call a method on those objects which squares the index. This approach is similar to how some other object oriented languages implement closures 'under the hood'.
 
<lang freebasic>' FB 1.05.0 Win64
 
Type Closure
Private:
index As Integer
Public:
Declare Constructor(index As Integer = 0)
Declare Function Square As Integer
End Type
 
Constructor Closure(index As Integer = 0)
This.index = index
End Constructor
 
Function Closure.Square As Integer
Return index * index
End Function
 
Dim a(1 To 10) As Closure
 
' create Closure objects which capture their index
For i As Integer = 1 To 10
a(i) = Closure(i)
Next
 
' call the Square method on all but the last object
For i As Integer = 1 to 9
Print a(i).Square
Next
 
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<pre>
1
4
9
16
25
36
49
64
81
</pre>
 
Line 835 ⟶ 784:
 
<lang forth>25</lang>
 
=={{header|FreeBASIC}}==
 
FreeBASIC doesn't support closures or anonymous methods, as such. However, what we can do is to create an array of objects to capture their index and then call a method on those objects which squares the index. This approach is similar to how some other object oriented languages implement closures 'under the hood'.
 
<lang freebasic>' FB 1.05.0 Win64
 
Type Closure
Private:
index As Integer
Public:
Declare Constructor(index As Integer = 0)
Declare Function Square As Integer
End Type
 
Constructor Closure(index As Integer = 0)
This.index = index
End Constructor
 
Function Closure.Square As Integer
Return index * index
End Function
 
Dim a(1 To 10) As Closure
 
' create Closure objects which capture their index
For i As Integer = 1 To 10
a(i) = Closure(i)
Next
 
' call the Square method on all but the last object
For i As Integer = 1 to 9
Print a(i).Square
Next
 
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<pre>
1
4
9
16
25
36
49
64
81
</pre>
 
=={{header|Go}}==
Line 1,455:
64
</pre>
 
=={{header|Perl 6}}==
{{Works with|Rakudo|2015.12}}
All blocks are anonymous closures in Perl 6, and parameters are lexicals, so it's easy to generate a list of them. We'll use a <tt>gather</tt>/<tt>take</tt> generator loop, and call the closures in random order, just to keep things interesting.
<lang perl6>my @c = gather for ^10 -> $i {
take { $i * $i }
}
 
.().say for @c.pick(*); # call them in random order</lang>
{{out}}
<pre>36
64
25
1
16
0
4
9
81
49</pre>
Or equivalently, using a more functional notation:
<lang perl6>say .() for pick *, map -> $i { -> {$i * $i} }, ^10</lang>
 
=={{header|Phix}}==
Line 1,827 ⟶ 1,805:
'(0 1 4 9 16 25 36 49 64 81)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|Rakudo|2015.12}}
All blocks are anonymous closures in Perl 6, and parameters are lexicals, so it's easy to generate a list of them. We'll use a <tt>gather</tt>/<tt>take</tt> generator loop, and call the closures in random order, just to keep things interesting.
<lang perl6>my @c = gather for ^10 -> $i {
take { $i * $i }
}
 
.().say for @c.pick(*); # call them in random order</lang>
{{out}}
<pre>36
64
25
1
16
0
4
9
81
49</pre>
Or equivalently, using a more functional notation:
<lang perl6>say .() for pick *, map -> $i { -> {$i * $i} }, ^10</lang>
 
=={{header|Red}}==
Line 1,920 ⟶ 1,921:
{{out}}
<pre>7th val: 49</pre>
 
=={{header|Scala}}==
<lang scala>val closures=for(i <- 0 to 9) yield (()=>i*i)
0 to 8 foreach (i=> println(closures(i)()))
println("---\n"+closures(7)())</lang>
{{out}}
<pre>0
1
4
9
16
25
36
49
64
---
49</pre>
 
=={{header|Scheme}}==
Line 1,950 ⟶ 1,968:
(newline)
</lang>
 
=={{header|Scala}}==
<lang scala>val closures=for(i <- 0 to 9) yield (()=>i*i)
0 to 8 foreach (i=> println(closures(i)()))
println("---\n"+closures(7)())</lang>
{{out}}
<pre>0
1
4
9
16
25
36
49
64
---
49</pre>
 
=={{header|Sidef}}==
Line 2,047 ⟶ 2,048:
val it = [0,1,4,9,16,25,36,49,64,81] : int list
</lang>
 
=={{header|Swift}}==
By default, Swift captures variables by reference. A naive implementation like the following C-style for loop does not work:
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.