Closures/Value capture: Difference between revisions

Content added Content deleted
m (Make Red entry a bit clearer)
(→‎{{header|REXX}}: re-worked the program to be in compliance with the task's requirements, added a seed (for the RANDOM BIF) so repeated executions can be scrutinized for different lists and base offsets.)
Line 1,666: Line 1,666:


=={{header|REXX}}==
=={{header|REXX}}==
This REXX version supports both a one─ and zero─based list   (it can be specified at the command line.)
<lang rexx>/*REXX program has a list of ten functions, each returns its invocation (index) squared.*/


The default is to use a zero─based list.
do j=1 for 9; ?=random(0, 9) /*invoke random functions nine times.*/
interpret 'CALL .'? /*invoke a randomly selected function. */
end /*j*/ /* [↑] the called function has no args*/


The list can also be specified at the command line. &nbsp; The default is an ordered list based on an obscure sequence &nbsp; (but puzzle enthusiasts can figure it out).
say 'The tenth invocation of .0 ───► ' .0()

No error checking is performed on the user input(s).
<lang rexx>/*REXX program has a list of ten functions, each returns its invocation (index) squared.*/
parse arg seed base $ /*obtain optional arguments from the CL*/
if datatype(seed, 'W') then call random ,,seed /*Not given? Use random start seed. */
if base=='' | base="," then base=0 /* " " Use a zero─based list. */
if $='' then $= 8 5 4 9 1 3 2 7 6 0 /* " " Use ordered function list*/
/*the $ list must contain 10 functions.*/
say 'the' word("zero one", base+1)'─based list is: ' $ /*show list of functions.*/
/*BASED must be either 1 or 0. */
?='.'random(0, 9) /*get a random name of a function. */
interpret 'CALL' ? /*invoke a randomly selected function. */
say 'function ' ? " returned " result /*display the value of random function.*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*───────────────────────────[Below is the closest thing to anonymous functions in REXX]*/
/*────────────────────────[Below are the closest things to anonymous functions in REXX].*/
.0: return .(); .1: return .(); .2: return .(); .3: return .(); .4: return .()
.0: return .(0) /*function .0 ─── bump its counter. */
.5: return .(); .6: return .(); .7: return .(); .8: return .(); .9: return .()
.1: return .(1) /* ' .1 " " " " */
.2: return .(2) /* ' .2 " " " " */
.3: return .(3) /* ' .3 " " " " */
.4: return .(4) /* ' .4 " " " " */
.5: return .(5) /* ' .5 " " " " */
.6: return .(6) /* ' .6 " " " " */
.7: return .(7) /* ' .7 " " " " */
.8: return .(8) /* ' .8 " " " " */
.9: return .(9) /* ' .9 " " " " */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.: if symbol('@')=="LIT" then @=0 /* ◄───handle very 1st invoke*/; @=@+1; return @*@</lang>
.: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2</lang>
{{out|output|text=&nbsp; when using the default input &nbsp; which assume a zero─based list):}}
'''output'''
<pre>
the zero─based list is: 8 5 4 9 1 3 2 7 6 0
function .0 returned 81
</pre>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> , &nbsp; 1 </tt>}}
<pre>
<pre>
the one─based list is: 8 5 4 9 1 3 2 7 6 0
The tenth invocation of .0 ───► 100
function .0 returned 100
</pre>
</pre>