Closures/Value capture: Difference between revisions

Content added Content deleted
(Added Io entry)
(Added Elixir)
Line 52: Line 52:
{{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}}==
Line 78: Line 77:


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}}==
Line 555: Line 553:
{{out}}
{{out}}
<pre>9</pre>
<pre>9</pre>

=={{header|Elixir}}==
<lang elixir>funs = for i <- 0..9, do: (fn -> i*i end)
Enum.each(funs, &IO.puts &1.())</lang>

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


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
Line 1,006: Line 1,022:


</lang>
</lang>

=={{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.