Task

Create a list of ten functions, in the simplest manner possible   (anonymous functions are encouraged),   such that the function at index    i     (you may choose to start     i     from either    0    or    1),    when run, should return the square of the index,   that is,     i  2.

Display the result of running any but the last function, to demonstrate that the function indeed remembers its value.



Goal

Demonstrate how to create a series of independent closures based on the same template but maintain separate copies of the variable closed over.

In imperative languages, one would generally use a loop with a mutable counter variable.

For each function to maintain the correct number, it has to capture the value of the variable at the time it was created, rather than just a reference to the variable, which would have a different value by the time the function was run.

See also: Multiple distinct objects

/*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. */
/*────────────────────────[Below are the closest things to anonymous functions in REXX].*/
.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