Closures/Value capture: Difference between revisions
Content added Content deleted
(→{{header|Clojure}}: Add implementation.) |
|||
Line 75: | Line 75: | ||
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|AppleScript}}== |
|||
{{trans|JavaScript}} |
|||
<lang AppleScript>on run |
|||
set fns to {} |
|||
repeat with i from 1 to 10 |
|||
set end of fns to closure(i) |
|||
end repeat |
|||
lambda() of item 3 of fns |
|||
end run |
|||
on closure(x) |
|||
script |
|||
on lambda() |
|||
return x * x |
|||
end lambda |
|||
end script |
|||
end closure</lang> |
|||
{{Out}} |
|||
<pre>9</pre> |
|||
=={{header|Axiom}}== |
=={{header|Axiom}}== |