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.) |
|||
⚫ | |||
The default is to use a zero─based list. |
|||
do j=1 for 9; ?=random(0, 9) /*invoke random functions nine times.*/ |
|||
⚫ | |||
⚫ | |||
The list can also be specified at the command line. The default is an ordered list based on an obscure sequence (but puzzle enthusiasts can figure it out). |
|||
say 'The tenth invocation of .0 ───► ' .0() |
|||
No error checking is performed on the user input(s). |
|||
⚫ | |||
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*/ |
|||
⚫ | |||
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. */ |
|||
⚫ | |||
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 are the closest things to anonymous functions in REXX].*/ |
||
.0: return .() |
.0: return .(0) /*function .0 ─── bump its counter. */ |
||
. |
.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 " " " " */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
.: |
.: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2</lang> |
||
{{out|output|text= when using the default input 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= when using the input of: <tt> , 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> |
||