Apply a callback to an array: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: used a single array for this demonstration.) |
|||
Line 2,575: | Line 2,575: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX |
<lang rexx>/*REXX program applies a callback to an array (using factorials for a demonstration).*/ |
||
numeric digits 100 /*be able to display some huge numbers.*/ |
|||
a.=; b.=; a.0 = 0 |
|||
parse arg # . /*obtain an optional value from the CL.*/ |
|||
a.= /*initialize the array A to all nulls*/ |
|||
if #=='' | #=="," then #= 12 /*Not assigned? Then use default value*/ |
|||
a. |
do j=0 to #; a.j= j /*assign the integer J ───► A.j */ |
||
end /*j*/ /*array A will have N values: 0 ──► #*/ |
|||
a.6 = 6 |
|||
call listA 'before callback' /*display A array before the callback*/ |
|||
a |
say /*display a blank line for readability.*/ |
||
say ' ··· applying callback to array A ···' /*display what is about to happen to B.*/ |
|||
a |
say /*display a blank line for readability.*/ |
||
call bangit 'a' /*factorialize (the values) of A array.*/ |
|||
call listAB 'before' |
|||
/* store the results ───► array B.*/ |
|||
call listA ' after callback' /*display A array after the callback.*/ |
|||
call listAB ' after' |
|||
exit /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
bangit: do |
bangit: do v=0; $= value(arg(1)'.'v); if $=='' then return /*No value? Then return*/ |
||
call value arg( |
call value arg(1)'.'v, fact($) /*assign a value (a factorial) to array*/ |
||
end /*i*/ |
end /*i*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
fact: procedure; !=1; do |
fact: procedure; arg x; != 1; do f=2 to x; != !*f; end; /*f*/; return ! |
||
⚫ | |||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
{{out|output|text= when using the default input:}} |
|||
⚫ | |||
do k=0 while b.k\==''; say arg(1) 'b.'k"="b.k; end /*k*/ |
|||
return</lang> |
|||
{{Out}} |
|||
<pre> |
<pre> |
||
before a.0=0 |
before callback a.0= 0 |
||
before a.1=1 |
before callback a.1= 1 |
||
before a.2=2 |
before callback a.2= 2 |
||
before a.3=3 |
before callback a.3= 3 |
||
before a.4=4 |
before callback a.4= 4 |
||
before a.5=5 |
before callback a.5= 5 |
||
before a.6=6 |
before callback a.6= 6 |
||
before a.7=7 |
before callback a.7= 7 |
||
before a.8=8 |
before callback a.8= 8 |
||
before a.9=9 |
before callback a.9= 9 |
||
before a.10=10 |
before callback a.10= 10 |
||
before callback a.11= 11 |
|||
before callback a.12= 12 |
|||
··· applying callback to array A ··· |
|||
after a.0=0 |
|||
after a.1=1 |
|||
after a.2=2 |
|||
after a.3=3 |
|||
after a.4=4 |
|||
after a.5=5 |
|||
after a.6=6 |
|||
after a.7=7 |
|||
after a.8=8 |
|||
after a.9=9 |
|||
after a.10=10 |
|||
after |
after callback a.0= 1 |
||
after |
after callback a.1= 1 |
||
after |
after callback a.2= 2 |
||
after |
after callback a.3= 6 |
||
after |
after callback a.4= 24 |
||
after |
after callback a.5= 120 |
||
after |
after callback a.6= 720 |
||
after |
after callback a.7= 5040 |
||
after |
after callback a.8= 40320 |
||
after |
after callback a.9= 362880 |
||
after |
after callback a.10= 3628800 |
||
after callback a.11= 39916800 |
|||
after callback a.12= 479001600 |
|||
</pre> |
</pre> |
||