Closures/Value capture: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1: Line 1:
[[Category:Functions and subroutines]]
{{task}}
{{task}}


Line 15: Line 16:


See also: [[Multiple distinct objects]]
See also: [[Multiple distinct objects]]

=={{header|11l}}==
=={{header|11l}}==
<syntaxhighlight lang="11l">[(() -> Int)] funcs
<syntaxhighlight lang="11l">[(() -> Int)] funcs
Line 26: Line 26:
9
9
</pre>
</pre>

=={{header|Ada}}==
=={{header|Ada}}==


Line 64: Line 63:
{{out}}
{{out}}
<pre> 1 4 9 16 25 36 49 64 81</pre>
<pre> 1 4 9 16 25 36 49 64 81</pre>

=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|2.8}}
{{works with|ALGOL 68G|2.8}}
Line 89: Line 87:


Using partial parametrization as proposed in Algol Bulletin by Charles Lindsey. Algol68G does not support binding ''all'' actual parameters "partially" without deproceduring, so a PROC(BOOL)INT mode is used instead of a PROC INT. The variable ''captured i'' is passed twice, once by reference and once by value, to demonstrate that it is possible to capture both ways, and a little extra code is added to show that the closure can modify the captured variable.
Using partial parametrization as proposed in Algol Bulletin by Charles Lindsey. Algol68G does not support binding ''all'' actual parameters "partially" without deproceduring, so a PROC(BOOL)INT mode is used instead of a PROC INT. The variable ''captured i'' is passed twice, once by reference and once by value, to demonstrate that it is possible to capture both ways, and a little extra code is added to show that the closure can modify the captured variable.

=={{header|AntLang}}==
=={{header|AntLang}}==
<syntaxhighlight lang="AntLang">fns: {n: x; {n expt 2}} map range[10]
<syntaxhighlight lang="AntLang">fns: {n: x; {n expt 2}} map range[10]
(8 elem fns)[]</syntaxhighlight>
(8 elem fns)[]</syntaxhighlight>

=={{header|AppleScript}}==
=={{header|AppleScript}}==
{{trans|JavaScript}}
{{trans|JavaScript}}
Line 174: Line 170:
{{Out}}
{{Out}}
<pre>9</pre>
<pre>9</pre>

=={{header|Axiom}}==
=={{header|Axiom}}==
Using the Spad compiler:
Using the Spad compiler:
Line 189: Line 184:
<syntaxhighlight lang="Axiom">[1,4,9,16,25,36,49,64,81,100]
<syntaxhighlight lang="Axiom">[1,4,9,16,25,36,49,64,81,100]
Type: List(Integer)</syntaxhighlight>
Type: List(Integer)</syntaxhighlight>

=={{header|Babel}}==
=={{header|Babel}}==


Line 217: Line 211:
Essentially, a function has been constructed for each value to be squared (10 down to 1). The cp operator ensures that we generate a fresh copy of the number to be squared, as well as the code for multiplying, {*}.
Essentially, a function has been constructed for each value to be squared (10 down to 1). The cp operator ensures that we generate a fresh copy of the number to be squared, as well as the code for multiplying, {*}.
In the final each loop, we eval each of the constructed functions and output the result.
In the final each loop, we eval each of the constructed functions and output the result.

=={{header|Bracmat}}==
=={{header|Bracmat}}==
<syntaxhighlight lang="bracmat">( -1:?i
<syntaxhighlight lang="bracmat">( -1:?i
Line 238: Line 231:
49
49
64</pre>
64</pre>

=={{header|C}}==
=={{header|C}}==


Line 353: Line 345:
0
0
</pre>
</pre>

=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
===Using Linq===
===Using Linq===
Line 418: Line 409:
49
49
64</syntaxhighlight>
64</syntaxhighlight>

=={{header|C++}}==
=={{header|C++}}==
{{works with|C++11}}
{{works with|C++11}}
Line 445: Line 435:
81
81
</pre>
</pre>

=={{header|Ceylon}}==
=={{header|Ceylon}}==
<syntaxhighlight lang="ceylon">shared void run() {
<syntaxhighlight lang="ceylon">shared void run() {
Line 456: Line 445:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Clojure}}==
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(def funcs (map #(fn [] (* % %)) (range 11)))
<syntaxhighlight lang="clojure">(def funcs (map #(fn [] (* % %)) (range 11)))
Line 463: Line 451:
<pre>9
<pre>9
16</pre>
16</pre>

=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==


Line 473: Line 460:
console.log func() for func in funcs
console.log func() for func in funcs
</syntaxhighlight>
</syntaxhighlight>

=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">CL-USER> (defparameter alist
<syntaxhighlight lang="lisp">CL-USER> (defparameter alist
Line 486: Line 472:


The ''loop'' mutates its binding ''i''. The purpose of <code>(let ((i i)) ...)</code> is to create a different binding ''i'' for each ''lambda'' to capture. Otherwise, all 10 lambdas would capture the same binding and return 100.
The ''loop'' mutates its binding ''i''. The purpose of <code>(let ((i i)) ...)</code> is to create a different binding ''i'' for each ''lambda'' to capture. Otherwise, all 10 lambdas would capture the same binding and return 100.

=={{header|D}}==
=={{header|D}}==
===Less Functional Version===
===Less Functional Version===
Line 509: Line 494:
{{out}}
{{out}}
<pre>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</pre>
<pre>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</pre>

=={{header|Delphi}}==
=={{header|Delphi}}==
{{works with|Delphi 2009}}
{{works with|Delphi 2009}}
Line 551: Line 535:
81
81
</pre>
</pre>

=={{header|Dyalect}}==
=={{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:
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:
Line 580: Line 563:


This is similar to a JavaScript (ES6) solution.
This is similar to a JavaScript (ES6) solution.

=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
<syntaxhighlight lang="scheme">
Line 588: Line 570:
→ 25
→ 25
</syntaxhighlight>
</syntaxhighlight>

=={{header|Elena}}==
=={{header|Elena}}==
ELENA 4.1 :
ELENA 4.1 :
Line 611: Line 592:
64
64
81</pre>
81</pre>

=={{header|Elixir}}==
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">funs = for i <- 0..9, do: (fn -> i*i end)
<syntaxhighlight lang="elixir">funs = for i <- 0..9, do: (fn -> i*i end)
Line 629: Line 609:
81
81
</pre>
</pre>

=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
As of Emacs 24.3, lexical closures are supported, therefore alleviating hacks such as lexical-let.
As of Emacs 24.3, lexical closures are supported, therefore alleviating hacks such as lexical-let.
Line 640: Line 619:
'(1 2 3 4 5 6 7 8 9 10)))
'(1 2 3 4 5 6 7 8 9 10)))
;; => (1 4 9 16 25 36 49 64 81 100)</syntaxhighlight>
;; => (1 4 9 16 25 36 49 64 81 100)</syntaxhighlight>

=={{header|Erlang}}==
=={{header|Erlang}}==
Erlang uses lexical scoping and has anonymous functions.
Erlang uses lexical scoping and has anonymous functions.
Line 672: Line 650:
ok
ok
</pre>
</pre>

=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Nearly identical to OCaml
Nearly identical to OCaml
Line 715: Line 692:
81
81
</pre>
</pre>

=={{header|Factor}}==
=={{header|Factor}}==
===Using lexical variables===
===Using lexical variables===
Line 756: Line 732:
] each
] each
drop</syntaxhighlight>
drop</syntaxhighlight>

=={{header|Fantom}}==
=={{header|Fantom}}==


Line 783: Line 758:
Function at index: 7 outputs 49
Function at index: 7 outputs 49
</pre>
</pre>

=={{header|Forth}}==
=={{header|Forth}}==
<syntaxhighlight lang="forth">: xt-array here { a }
<syntaxhighlight lang="forth">: xt-array here { a }
Line 795: Line 769:


<syntaxhighlight lang="forth">25</syntaxhighlight>
<syntaxhighlight lang="forth">25</syntaxhighlight>

=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==


Line 846: Line 819:
81
81
</pre>
</pre>

=={{header|Go}}==
=={{header|Go}}==
<syntaxhighlight lang="go">package main
<syntaxhighlight lang="go">package main
Line 868: Line 840:
func #3: 9
func #3: 9
</pre>
</pre>

=={{header|Groovy}}==
=={{header|Groovy}}==
Solution:
Solution:
Line 881: Line 852:
{{out}}
{{out}}
<pre>49</pre>
<pre>49</pre>

=={{header|Haskell}}==
=={{header|Haskell}}==


Line 906: Line 876:
> fs !! 8 $ undefined
> fs !! 8 $ undefined
81</syntaxhighlight>
81</syntaxhighlight>

=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
This uses Unicon specific calling sequences for co-expressions. It can be made to run under Icon by modifying the calling syntax.
This uses Unicon specific calling sequences for co-expressions. It can be made to run under Icon by modifying the calling syntax.
Line 933: Line 902:
{{out}}
{{out}}
<pre>Randomly selecting L[8] = 64</pre>
<pre>Randomly selecting L[8] = 64</pre>

=={{header|Io}}==
=={{header|Io}}==
<syntaxhighlight lang="text">blist := list(0,1,2,3,4,5,6,7,8,9) map(i,block(i,block(i*i)) call(i))
<syntaxhighlight lang="text">blist := list(0,1,2,3,4,5,6,7,8,9) map(i,block(i,block(i*i)) call(i))
writeln(blist at(3) call) // prints 9</syntaxhighlight>
writeln(blist at(3) call) // prints 9</syntaxhighlight>

=={{header|J}}==
=={{header|J}}==


Line 986: Line 953:
{::&VL 5 '' NB. Invoking the 6th verb with a dummy argument ('')
{::&VL 5 '' NB. Invoking the 6th verb with a dummy argument ('')
25</syntaxhighlight>
25</syntaxhighlight>

=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|8+}}
{{works with|Java|8+}}
Line 1,023: Line 989:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|JavaScript}}==
=={{header|JavaScript}}==


Line 1,093: Line 1,058:
9
9
</pre>
</pre>

=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang="julia">funcs = [ () -> i^2 for i = 1:10 ]</syntaxhighlight>
<syntaxhighlight lang="julia">funcs = [ () -> i^2 for i = 1:10 ]</syntaxhighlight>
Line 1,101: Line 1,065:
49
49
</pre>
</pre>

=={{header|Kotlin}}==
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6
Line 1,124: Line 1,087:
64
64
</pre>
</pre>

=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==


Line 1,140: Line 1,102:
-> 16
-> 16
</syntaxhighlight>
</syntaxhighlight>

=={{header|Latitude}}==
=={{header|Latitude}}==


Line 1,163: Line 1,124:
64
64
81</pre>
81</pre>

=={{header|LFE}}==
=={{header|LFE}}==


Line 1,193: Line 1,153:


</syntaxhighlight>
</syntaxhighlight>

=={{header|Lingo}}==
=={{header|Lingo}}==


Line 1,249: Line 1,208:
put call(funcs[7], _movie, 4, 5, 6)
put call(funcs[7], _movie, 4, 5, 6)
-- 64</syntaxhighlight>
-- 64</syntaxhighlight>

=={{header|Logtalk}}==
=={{header|Logtalk}}==
The example that follow uses Logtalk's native support for lambda expressions.
The example that follow uses Logtalk's native support for lambda expressions.
Line 1,284: Line 1,242:
yes
yes
</syntaxhighlight>
</syntaxhighlight>

=={{header|Lua}}==
=={{header|Lua}}==
<syntaxhighlight lang="Lua">
<syntaxhighlight lang="Lua">
Line 1,298: Line 1,255:
9
9
</pre>
</pre>

=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="M2000 Interpreter">
<syntaxhighlight lang="M2000 Interpreter">
Line 1,363: Line 1,319:


</syntaxhighlight>
</syntaxhighlight>

=={{header|Maple}}==
=={{header|Maple}}==
<syntaxhighlight lang="Maple">> L := map( i -> (() -> i^2), [seq](1..10) ):
<syntaxhighlight lang="Maple">> L := map( i -> (() -> i^2), [seq](1..10) ):
Line 1,371: Line 1,326:
16
16
</syntaxhighlight>
</syntaxhighlight>

=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="Mathematica">Function[i, i^2 &] /@ Range@10
<syntaxhighlight lang="Mathematica">Function[i, i^2 &] /@ Range@10
Line 1,378: Line 1,332:
%[[2]][]
%[[2]][]
->4</syntaxhighlight>
->4</syntaxhighlight>

=={{header|Nemerle}}==
=={{header|Nemerle}}==
<syntaxhighlight lang="Nemerle">using System.Console;
<syntaxhighlight lang="Nemerle">using System.Console;
Line 1,396: Line 1,349:
<pre>16
<pre>16
4</pre>
4</pre>

=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang="nim">var funcs: seq[proc(): int] = @[]
<syntaxhighlight lang="nim">var funcs: seq[proc(): int] = @[]
Line 1,407: Line 1,359:
for i in 0..8:
for i in 0..8:
echo "func[", i, "]: ", funcs[i]()</syntaxhighlight>
echo "func[", i, "]: ", funcs[i]()</syntaxhighlight>

=={{header|Objeck}}==
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">use Collection.Generic;
<syntaxhighlight lang="objeck">use Collection.Generic;
Line 1,440: Line 1,391:
81
81
</pre>
</pre>

=={{header|Objective-C}}==
=={{header|Objective-C}}==
{{works with|Cocoa|Mac OS X 10.6+}} with ARC
{{works with|Cocoa|Mac OS X 10.6+}} with ARC
Line 1,451: Line 1,401:
NSLog(@"%d", foo()); // logs "9"
NSLog(@"%d", foo()); // logs "9"
</syntaxhighlight>
</syntaxhighlight>

=={{header|OCaml}}==
=={{header|OCaml}}==


Line 1,473: Line 1,422:
fun.(6) = 36
fun.(6) = 36
</pre>
</pre>

=={{header|Oforth}}==
=={{header|Oforth}}==
<syntaxhighlight lang="Oforth">: newClosure(i) #[ i sq ] ;
<syntaxhighlight lang="Oforth">: newClosure(i) #[ i sq ] ;
Line 1,482: Line 1,430:
49
49
</pre>
</pre>

=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
{{works with|PARI/GP|2.4.2 and above}}
{{works with|PARI/GP|2.4.2 and above}}
Line 1,489: Line 1,436:
{{out}}
{{out}}
<pre>%1 = 25</pre>
<pre>%1 = 25</pre>

=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight lang="perl">my @f = map(sub { $_ * $_ }, 0 .. 9); # @f is an array of subs
<syntaxhighlight lang="perl">my @f = map(sub { $_ * $_ }, 0 .. 9); # @f is an array of subs
Line 1,505: Line 1,451:
64
64
</pre>
</pre>

=={{header|Phix}}==
=={{header|Phix}}==
Phix does not support closures, but they seem easy enough to emulate
Phix does not support closures, but they seem easy enough to emulate
Line 1,576: Line 1,521:
<!--</syntaxhighlight>-->
<!--</syntaxhighlight>-->
same output, for both tests
same output, for both tests

=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">def power2
<syntaxhighlight lang="Phixmonti">def power2
Line 1,595: Line 1,539:
i get i swap exec print " " print
i get i swap exec print " " print
endfor</syntaxhighlight>
endfor</syntaxhighlight>

=={{header|PHP}}==
=={{header|PHP}}==
{{works with|PHP|5.3+}}
{{works with|PHP|5.3+}}
Line 1,615: Line 1,558:
echo $funcs[3](), "\n"; // prints 9
echo $funcs[3](), "\n"; // prints 9
?></syntaxhighlight>
?></syntaxhighlight>

=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<syntaxhighlight lang="PicoLisp">(setq FunList
<syntaxhighlight lang="PicoLisp">(setq FunList
Line 1,627: Line 1,569:
: ((get FunList 8))
: ((get FunList 8))
-> 64</pre>
-> 64</pre>

=={{header|Pike}}==
=={{header|Pike}}==
<syntaxhighlight lang="Pike">array funcs = ({});
<syntaxhighlight lang="Pike">array funcs = ({});
Line 1,642: Line 1,583:
});
});
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|PowerShell}}==
=={{header|PowerShell}}==
I'm not sure that I understood the question/task. This task seems to be the same as the 'Accumulator Factory' task.
I'm not sure that I understood the question/task. This task seems to be the same as the 'Accumulator Factory' task.
Line 1,705: Line 1,645:
20 400
20 400
</pre>
</pre>

=={{header|Prolog}}==
=={{header|Prolog}}==
Works with SWI-Prolog and module '''lambda.pl''' from '''Ulrich Neumerkel'''. <br>
Works with SWI-Prolog and module '''lambda.pl''' from '''Ulrich Neumerkel'''. <br>
Line 1,739: Line 1,678:
true.
true.
</pre>
</pre>

=={{header|Python}}==
=={{header|Python}}==
The naive way does not work:
The naive way does not work:
Line 1,772: Line 1,710:
<syntaxhighlight lang="python">funcs=[eval("lambda:%s"%i**2)for i in range(10)]
<syntaxhighlight lang="python">funcs=[eval("lambda:%s"%i**2)for i in range(10)]
print funcs[3]() # prints 9</syntaxhighlight>
print funcs[3]() # prints 9</syntaxhighlight>

=={{header|Quackery}}==
=={{header|Quackery}}==


Line 1,788: Line 1,725:


<pre>25</pre>
<pre>25</pre>

=={{header|R}}==
=={{header|R}}==


Line 1,866: Line 1,802:
[1] 16
[1] 16
</pre>
</pre>

=={{header|Racket}}==
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<syntaxhighlight lang="racket">
Line 1,877: Line 1,812:
'(0 1 4 9 16 25 36 49 64 81)
'(0 1 4 9 16 25 36 49 64 81)
</syntaxhighlight>
</syntaxhighlight>

=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
Line 1,900: Line 1,834:
Or equivalently, using a more functional notation:
Or equivalently, using a more functional notation:
<syntaxhighlight lang="raku" line>say .() for pick *, map -> $i { -> {$i * $i} }, ^10</syntaxhighlight>
<syntaxhighlight lang="raku" line>say .() for pick *, map -> $i { -> {$i * $i} }, ^10</syntaxhighlight>

=={{header|Red}}==
=={{header|Red}}==
<syntaxhighlight lang="Red">
<syntaxhighlight lang="Red">
Line 1,908: Line 1,841:
== 49
== 49
</syntaxhighlight>
</syntaxhighlight>

=={{header|REXX}}==
=={{header|REXX}}==
This REXX version supports both a one─ and zero─based list &nbsp; (it can be specified at the command line.)
This REXX version supports both a one─ and zero─based list &nbsp; (it can be specified at the command line.)
Line 1,952: Line 1,884:
function .0 returned 100
function .0 returned 100
</pre>
</pre>

=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang="ring">
<syntaxhighlight lang="ring">
Line 1,975: Line 1,906:
49
49
</pre>
</pre>

=={{header|Ruby}}==
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">procs = Array.new(10){|i| ->{i*i} } # -> creates a lambda
<syntaxhighlight lang="ruby">procs = Array.new(10){|i| ->{i*i} } # -> creates a lambda
Line 1,981: Line 1,911:


In Ruby, lambdas (and procs) are closures.
In Ruby, lambdas (and procs) are closures.

=={{header|Rust}}==
=={{header|Rust}}==
One note here about referencing values and capturing values: <br>
One note here about referencing values and capturing values: <br>
Line 1,993: Line 1,922:
{{out}}
{{out}}
<pre>7th val: 49</pre>
<pre>7th val: 49</pre>

=={{header|Scala}}==
=={{header|Scala}}==
<syntaxhighlight lang="scala">val closures=for(i <- 0 to 9) yield (()=>i*i)
<syntaxhighlight lang="scala">val closures=for(i <- 0 to 9) yield (()=>i*i)
Line 2,010: Line 1,938:
---
---
49</pre>
49</pre>

=={{header|Scheme}}==
=={{header|Scheme}}==


Line 2,040: Line 1,967:
(newline)
(newline)
</syntaxhighlight>
</syntaxhighlight>

=={{header|Sidef}}==
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var f = (
<syntaxhighlight lang="ruby">var f = (
Line 2,082: Line 2,008:
81
81
</pre>
</pre>

=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
<syntaxhighlight lang="smalltalk">funcs := (1 to: 10) collect: [ :i | [ i * i ] ] .
<syntaxhighlight lang="smalltalk">funcs := (1 to: 10) collect: [ :i | [ i * i ] ] .
Line 2,088: Line 2,013:
{{out}}
{{out}}
<pre>9</pre>
<pre>9</pre>

=={{header|Sparkling}}==
=={{header|Sparkling}}==
In Sparkling, upvalues (variables in the closure) are captured by value.
In Sparkling, upvalues (variables in the closure) are captured by value.
Line 2,112: Line 2,036:
print(fnlist[3]()); // prints 9
print(fnlist[3]()); // prints 9
print(fnlist[5]()); // prints 25</syntaxhighlight>
print(fnlist[5]()); // prints 25</syntaxhighlight>

=={{header|Standard ML}}==
=={{header|Standard ML}}==
<syntaxhighlight lang="Standard ML">
<syntaxhighlight lang="Standard ML">
Line 2,120: Line 2,043:
val it = [0,1,4,9,16,25,36,49,64,81] : int list
val it = [0,1,4,9,16,25,36,49,64,81] : int list
</syntaxhighlight>
</syntaxhighlight>

=={{header|Swift}}==
=={{header|Swift}}==
By default, Swift captures variables by reference. A naive implementation like the following C-style for loop does not work:
By default, Swift captures variables by reference. A naive implementation like the following C-style for loop does not work:
Line 2,146: Line 2,068:
<syntaxhighlight lang="swift">let funcs = [] + map(0..<10) {i in { i * i }}
<syntaxhighlight lang="swift">let funcs = [] + map(0..<10) {i in { i * i }}
println(funcs[3]()) // prints 9</syntaxhighlight>
println(funcs[3]()) // prints 9</syntaxhighlight>

=={{header|Tcl}}==
=={{header|Tcl}}==
Tcl does not support closures (either value-capturing or variable-capturing) by default, but value-capturing closures are easy to emulate.
Tcl does not support closures (either value-capturing or variable-capturing) by default, but value-capturing closures are easy to emulate.
Line 2,193: Line 2,114:
8=>64
8=>64
</pre>
</pre>

=={{header|TXR}}==
=={{header|TXR}}==


Line 2,253: Line 2,173:


Whenever we call a continuation, the <code>(block sqr ...)</code> environment is restored. and the suspended computation inside the block resumes by returning out of the <code>(suspend ...)</code> form normally. The block then executes to completion, returning the <code>(* cap cap)</code> form's value. At that point, our call to the continuation terminates, yielding that value.
Whenever we call a continuation, the <code>(block sqr ...)</code> environment is restored. and the suspended computation inside the block resumes by returning out of the <code>(suspend ...)</code> form normally. The block then executes to completion, returning the <code>(* cap cap)</code> form's value. At that point, our call to the continuation terminates, yielding that value.

=={{header|Wren}}==
=={{header|Wren}}==
<syntaxhighlight lang="ecmascript">var fs = List.filled(10, null)
<syntaxhighlight lang="ecmascript">var fs = List.filled(10, null)
Line 2,274: Line 2,193:
Function #8: 64
Function #8: 64
</pre>
</pre>

=={{header|Yabasic}}==
=={{header|Yabasic}}==
<syntaxhighlight lang="Yabasic">
<syntaxhighlight lang="Yabasic">
Line 2,291: Line 2,209:
next
next
</syntaxhighlight>
</syntaxhighlight>

=={{header|zkl}}==
=={{header|zkl}}==
Create a closure of the index over a square function
Create a closure of the index over a square function
Line 2,311: Line 2,228:
L(0,1,4,9,16,25,36,49,64,81)
L(0,1,4,9,16,25,36,49,64,81)
</pre>
</pre>

{{omit from|BASIC}}
{{omit from|BASIC}}
{{omit from|Brlcad}}
{{omit from|Brlcad}}
Line 2,318: Line 2,234:
{{omit from|PureBasic}}
{{omit from|PureBasic}}
{{omit from|ZX Spectrum Basic}}
{{omit from|ZX Spectrum Basic}}

[[Category:Functions and subroutines]]