Closures/Value capture: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1:
[[Category:Functions and subroutines]]
{{task}}
 
Line 15 ⟶ 16:
 
See also: [[Multiple distinct objects]]
 
=={{header|11l}}==
<syntaxhighlight lang="11l">[(() -> Int)] funcs
Line 26:
9
</pre>
 
=={{header|Ada}}==
 
Line 64 ⟶ 63:
{{out}}
<pre> 1 4 9 16 25 36 49 64 81</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|2.8}}
Line 89 ⟶ 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.
 
=={{header|AntLang}}==
<syntaxhighlight lang="AntLang">fns: {n: x; {n expt 2}} map range[10]
(8 elem fns)[]</syntaxhighlight>
 
=={{header|AppleScript}}==
{{trans|JavaScript}}
Line 174 ⟶ 170:
{{Out}}
<pre>9</pre>
 
=={{header|Axiom}}==
Using the Spad compiler:
Line 189 ⟶ 184:
<syntaxhighlight lang="Axiom">[1,4,9,16,25,36,49,64,81,100]
Type: List(Integer)</syntaxhighlight>
 
=={{header|Babel}}==
 
Line 217 ⟶ 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, {*}.
In the final each loop, we eval each of the constructed functions and output the result.
 
=={{header|Bracmat}}==
<syntaxhighlight lang="bracmat">( -1:?i
Line 238 ⟶ 231:
49
64</pre>
 
=={{header|C}}==
 
Line 353 ⟶ 345:
0
</pre>
 
=={{header|C sharp|C#}}==
===Using Linq===
Line 418 ⟶ 409:
49
64</syntaxhighlight>
 
=={{header|C++}}==
{{works with|C++11}}
Line 445 ⟶ 435:
81
</pre>
 
=={{header|Ceylon}}==
<syntaxhighlight lang="ceylon">shared void run() {
Line 456 ⟶ 445:
}
}</syntaxhighlight>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(def funcs (map #(fn [] (* % %)) (range 11)))
Line 463 ⟶ 451:
<pre>9
16</pre>
 
=={{header|CoffeeScript}}==
 
Line 473 ⟶ 460:
console.log func() for func in funcs
</syntaxhighlight>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">CL-USER> (defparameter alist
Line 486 ⟶ 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.
 
=={{header|D}}==
===Less Functional Version===
Line 509 ⟶ 494:
{{out}}
<pre>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</pre>
 
=={{header|Delphi}}==
{{works with|Delphi 2009}}
Line 551 ⟶ 535:
81
</pre>
 
=={{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:
Line 580 ⟶ 563:
 
This is similar to a JavaScript (ES6) solution.
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
Line 588 ⟶ 570:
→ 25
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 4.1 :
Line 611 ⟶ 592:
64
81</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">funs = for i <- 0..9, do: (fn -> i*i end)
Line 629 ⟶ 609:
81
</pre>
 
=={{header|Emacs Lisp}}==
As of Emacs 24.3, lexical closures are supported, therefore alleviating hacks such as lexical-let.
Line 640 ⟶ 619:
'(1 2 3 4 5 6 7 8 9 10)))
;; => (1 4 9 16 25 36 49 64 81 100)</syntaxhighlight>
 
=={{header|Erlang}}==
Erlang uses lexical scoping and has anonymous functions.
Line 672 ⟶ 650:
ok
</pre>
 
=={{header|F_Sharp|F#}}==
Nearly identical to OCaml
Line 715 ⟶ 692:
81
</pre>
 
=={{header|Factor}}==
===Using lexical variables===
Line 756 ⟶ 732:
] each
drop</syntaxhighlight>
 
=={{header|Fantom}}==
 
Line 783 ⟶ 758:
Function at index: 7 outputs 49
</pre>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: xt-array here { a }
Line 795 ⟶ 769:
 
<syntaxhighlight lang="forth">25</syntaxhighlight>
 
=={{header|FreeBASIC}}==
 
Line 846 ⟶ 819:
81
</pre>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
Line 868 ⟶ 840:
func #3: 9
</pre>
 
=={{header|Groovy}}==
Solution:
Line 881 ⟶ 852:
{{out}}
<pre>49</pre>
 
=={{header|Haskell}}==
 
Line 906 ⟶ 876:
> fs !! 8 $ undefined
81</syntaxhighlight>
 
=={{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.
Line 933 ⟶ 902:
{{out}}
<pre>Randomly selecting L[8] = 64</pre>
 
=={{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))
writeln(blist at(3) call) // prints 9</syntaxhighlight>
 
=={{header|J}}==
 
Line 986 ⟶ 953:
{::&VL 5 '' NB. Invoking the 6th verb with a dummy argument ('')
25</syntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|8+}}
Line 1,023 ⟶ 989:
}
}</syntaxhighlight>
 
=={{header|JavaScript}}==
 
Line 1,093 ⟶ 1,058:
9
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">funcs = [ () -> i^2 for i = 1:10 ]</syntaxhighlight>
Line 1,101 ⟶ 1,065:
49
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.6
Line 1,124 ⟶ 1,087:
64
</pre>
 
=={{header|Lambdatalk}}==
 
Line 1,140 ⟶ 1,102:
-> 16
</syntaxhighlight>
 
=={{header|Latitude}}==
 
Line 1,163 ⟶ 1,124:
64
81</pre>
 
=={{header|LFE}}==
 
Line 1,193 ⟶ 1,153:
 
</syntaxhighlight>
 
=={{header|Lingo}}==
 
Line 1,249 ⟶ 1,208:
put call(funcs[7], _movie, 4, 5, 6)
-- 64</syntaxhighlight>
 
=={{header|Logtalk}}==
The example that follow uses Logtalk's native support for lambda expressions.
Line 1,284 ⟶ 1,242:
yes
</syntaxhighlight>
 
=={{header|Lua}}==
<syntaxhighlight lang="Lua">
Line 1,298 ⟶ 1,255:
9
</pre>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="M2000 Interpreter">
Line 1,363 ⟶ 1,319:
 
</syntaxhighlight>
 
=={{header|Maple}}==
<syntaxhighlight lang="Maple">> L := map( i -> (() -> i^2), [seq](1..10) ):
Line 1,371 ⟶ 1,326:
16
</syntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="Mathematica">Function[i, i^2 &] /@ Range@10
Line 1,378 ⟶ 1,332:
%[[2]][]
->4</syntaxhighlight>
 
=={{header|Nemerle}}==
<syntaxhighlight lang="Nemerle">using System.Console;
Line 1,396 ⟶ 1,349:
<pre>16
4</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">var funcs: seq[proc(): int] = @[]
Line 1,407 ⟶ 1,359:
for i in 0..8:
echo "func[", i, "]: ", funcs[i]()</syntaxhighlight>
 
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">use Collection.Generic;
Line 1,440 ⟶ 1,391:
81
</pre>
 
=={{header|Objective-C}}==
{{works with|Cocoa|Mac OS X 10.6+}} with ARC
Line 1,451 ⟶ 1,401:
NSLog(@"%d", foo()); // logs "9"
</syntaxhighlight>
 
=={{header|OCaml}}==
 
Line 1,473 ⟶ 1,422:
fun.(6) = 36
</pre>
 
=={{header|Oforth}}==
<syntaxhighlight lang="Oforth">: newClosure(i) #[ i sq ] ;
Line 1,482 ⟶ 1,430:
49
</pre>
 
=={{header|PARI/GP}}==
{{works with|PARI/GP|2.4.2 and above}}
Line 1,489 ⟶ 1,436:
{{out}}
<pre>%1 = 25</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">my @f = map(sub { $_ * $_ }, 0 .. 9); # @f is an array of subs
Line 1,505 ⟶ 1,451:
64
</pre>
 
=={{header|Phix}}==
Phix does not support closures, but they seem easy enough to emulate
Line 1,576 ⟶ 1,521:
<!--</syntaxhighlight>-->
same output, for both tests
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">def power2
Line 1,595 ⟶ 1,539:
i get i swap exec print " " print
endfor</syntaxhighlight>
 
=={{header|PHP}}==
{{works with|PHP|5.3+}}
Line 1,615 ⟶ 1,558:
echo $funcs[3](), "\n"; // prints 9
?></syntaxhighlight>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="PicoLisp">(setq FunList
Line 1,627 ⟶ 1,569:
: ((get FunList 8))
-> 64</pre>
 
=={{header|Pike}}==
<syntaxhighlight lang="Pike">array funcs = ({});
Line 1,642 ⟶ 1,583:
});
}</syntaxhighlight>
 
=={{header|PowerShell}}==
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 ⟶ 1,645:
20 400
</pre>
 
=={{header|Prolog}}==
Works with SWI-Prolog and module '''lambda.pl''' from '''Ulrich Neumerkel'''. <br>
Line 1,739 ⟶ 1,678:
true.
</pre>
 
=={{header|Python}}==
The naive way does not work:
Line 1,772 ⟶ 1,710:
<syntaxhighlight lang="python">funcs=[eval("lambda:%s"%i**2)for i in range(10)]
print funcs[3]() # prints 9</syntaxhighlight>
 
=={{header|Quackery}}==
 
Line 1,788 ⟶ 1,725:
 
<pre>25</pre>
 
=={{header|R}}==
 
Line 1,866 ⟶ 1,802:
[1] 16
</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
Line 1,877 ⟶ 1,812:
'(0 1 4 9 16 25 36 49 64 81)
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
Line 1,900 ⟶ 1,834:
Or equivalently, using a more functional notation:
<syntaxhighlight lang="raku" line>say .() for pick *, map -> $i { -> {$i * $i} }, ^10</syntaxhighlight>
 
=={{header|Red}}==
<syntaxhighlight lang="Red">
Line 1,908 ⟶ 1,841:
== 49
</syntaxhighlight>
 
=={{header|REXX}}==
This REXX version supports both a one─ and zero─based list &nbsp; (it can be specified at the command line.)
Line 1,952 ⟶ 1,884:
function .0 returned 100
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
Line 1,975 ⟶ 1,906:
49
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">procs = Array.new(10){|i| ->{i*i} } # -> creates a lambda
Line 1,981 ⟶ 1,911:
 
In Ruby, lambdas (and procs) are closures.
 
=={{header|Rust}}==
One note here about referencing values and capturing values: <br>
Line 1,993 ⟶ 1,922:
{{out}}
<pre>7th val: 49</pre>
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">val closures=for(i <- 0 to 9) yield (()=>i*i)
Line 2,010 ⟶ 1,938:
---
49</pre>
 
=={{header|Scheme}}==
 
Line 2,040 ⟶ 1,967:
(newline)
</syntaxhighlight>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var f = (
Line 2,082 ⟶ 2,008:
81
</pre>
 
=={{header|Smalltalk}}==
<syntaxhighlight lang="smalltalk">funcs := (1 to: 10) collect: [ :i | [ i * i ] ] .
Line 2,088 ⟶ 2,013:
{{out}}
<pre>9</pre>
 
=={{header|Sparkling}}==
In Sparkling, upvalues (variables in the closure) are captured by value.
Line 2,112 ⟶ 2,036:
print(fnlist[3]()); // prints 9
print(fnlist[5]()); // prints 25</syntaxhighlight>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="Standard ML">
Line 2,120 ⟶ 2,043:
val it = [0,1,4,9,16,25,36,49,64,81] : int list
</syntaxhighlight>
 
=={{header|Swift}}==
By default, Swift captures variables by reference. A naive implementation like the following C-style for loop does not work:
Line 2,146 ⟶ 2,068:
<syntaxhighlight lang="swift">let funcs = [] + map(0..<10) {i in { i * i }}
println(funcs[3]()) // prints 9</syntaxhighlight>
 
=={{header|Tcl}}==
Tcl does not support closures (either value-capturing or variable-capturing) by default, but value-capturing closures are easy to emulate.
Line 2,193 ⟶ 2,114:
8=>64
</pre>
 
=={{header|TXR}}==
 
Line 2,253 ⟶ 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.
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascript">var fs = List.filled(10, null)
Line 2,274 ⟶ 2,193:
Function #8: 64
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="Yabasic">
Line 2,291 ⟶ 2,209:
next
</syntaxhighlight>
 
=={{header|zkl}}==
Create a closure of the index over a square function
Line 2,311 ⟶ 2,228:
L(0,1,4,9,16,25,36,49,64,81)
</pre>
 
{{omit from|BASIC}}
{{omit from|Brlcad}}
Line 2,318 ⟶ 2,234:
{{omit from|PureBasic}}
{{omit from|ZX Spectrum Basic}}
 
[[Category:Functions and subroutines]]
10,333

edits