Closures/Value capture: Difference between revisions
Content added Content deleted
m (omit from Ada) |
(Updated second D entry, tags) |
||
Line 220: | Line 220: | ||
=={{header|D}}== |
=={{header|D}}== |
||
===Less Functional Version=== |
|||
<lang d>import std.stdio; |
<lang d>import std.stdio; |
||
Line 230: | Line 231: | ||
writeln(funcs[3]()); |
writeln(funcs[3]()); |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>9</pre> |
<pre>9</pre> |
||
More |
===More Functional Version=== |
||
<lang d>import std.stdio, std.range, std.algorithm; |
<lang d>import std.stdio, std.range, std.algorithm; |
||
void main() { |
void main() { |
||
auto funcs = map!(i => () => i*i)( |
auto funcs = iota(10).map!(i => () => i*i)(); |
||
funcs.map!q{ a() }().writeln(); |
|||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</pre> |
<pre>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</pre> |
||