Generator/Exponential: Difference between revisions

Content added Content deleted
(add scala)
m (→‎{{header|REXX}}: deleted a superflous blank lines. -- ~~~~)
Line 1,420: Line 1,420:
<br>A lot of unnecessary iterator calls could be eliminated (and making the program a lot faster) if a check would be made to determine if a value has been emitted.
<br>A lot of unnecessary iterator calls could be eliminated (and making the program a lot faster) if a check would be made to determine if a value has been emitted.
<br>The REXX program can handle any numbers, not just positive integers; one only need to increase the size of the DIGITS if more digits are needed for extra-large numbers.
<br>The REXX program can handle any numbers, not just positive integers; one only need to increase the size of the DIGITS if more digits are needed for extra-large numbers.
<lang rexx>/*REXX program to show how to use a generator (also known as iterators).*/
<lang rexx>
/*REXX program to show how to use a generator (also known as iterators).*/

numeric digits 10000 /*just in case we need big 'uns. */
numeric digits 10000 /*just in case we need big 'uns. */
gen_.= /*have generators from scratch. */
gen_.= /*have generators from scratch. */
/*how_many: show that many vals.*/
/*how_many: show that many vals.*/
parse arg how_many; how_many=pick1(how_many 0)
parse arg how_many; how_many=pick1(how_many 0)
do j=1 for how_many; call tell 'squares' ,gen_squares(j) ; end

do j=1 to how_many; call tell 'squares' ,gen_squares(j) ; end
do j=1 for how_many; call tell 'cubes' ,gen_cubes(j) ; end
do j=1 to how_many; call tell 'cubes' ,gen_cubes(j) ; end
do j=1 for how_many; call tell 'sq¬cubes',gen_sqNcubes(j) ; end
if how_many>0 then say 'dropping 1st --> 20th values.'
do j=1 to how_many; call tell 'sq¬cubes',gen_sqNcubes(j) ; end
do j=1 for 20; drop gen_.sqNcubes.j ; end
if how_many>0 then say 'dropping 1st --> 20th values.'
do j=1 to 20; drop gen_.sqNcubes.j ; end
do j=20+1 for 10 ; call tell 'sq¬cubes',gen_sqNcubes(j) ; end
do j=20+1 for 10 ; call tell 'sq¬cubes',gen_sqNcubes(j) ; end

exit
exit

/*─────────────────────────────────────generate powers iterator─────────*/
/*─────────────────────────────────────generate powers iterator─────────*/
gen_powers: procedure expose gen_.; parse arg x,p; if x=='' then return
gen_powers: procedure expose gen_.; parse arg x,p; if x=='' then return
if gen_.powers.x.p=='' then gen_.powers.x.p=x**p
if gen_.powers.x.p=='' then gen_.powers.x.p=x**p
return gen_.powers.x.p
return gen_.powers.x.p

/*─────────────────────────────────────gen squares iterator─────────────*/
/*─────────────────────────────────────gen squares iterator─────────────*/
gen_squares: procedure expose gen_.; parse arg x;if x=='' then return
gen_squares: procedure expose gen_.; parse arg x;if x=='' then return
Line 1,449: Line 1,443:
end
end
return gen_.squares.x
return gen_.squares.x

/*─────────────────────────────────────gen cubes iterator───────────────*/
/*─────────────────────────────────────gen cubes iterator───────────────*/
gen_cubes: procedure expose gen_.; parse arg x; if x=='' then return
gen_cubes: procedure expose gen_.; parse arg x; if x=='' then return
Line 1,457: Line 1,450:
end
end
return gen_.cubes.x
return gen_.cubes.x

/*─────────────────────────────────────gen squares not cubes iterator───*/
/*─────────────────────────────────────gen squares not cubes iterator───*/
gen_sqNcubes: procedure expose gen_.; parse arg x; if x=='' then return
gen_sqNcubes: procedure expose gen_.; parse arg x; if x=='' then return
Line 1,477: Line 1,469:
end /*j*/
end /*j*/
return gen_.sqNcubes.x
return gen_.sqNcubes.x

/*─────────────────────────────────────"1─liner" subroutines────────────*/
/*─────────────────────────────────────"1─liner" subroutines────────────*/
tell: if j==1 then say /*format args to be aligned up. */
tell: if j==1 then say /*format args to be aligned up. */
say right(arg(1),20) right(j,5) right(arg(2),20); return
say right(arg(1),20) right(j,5) right(arg(2),20); return
pick1: return word(arg(1) arg(2),1)</lang>

'''output''' when using the default (no input)
pick1: return word(arg(1) arg(2),1)
<pre style="overflow:scroll">
</lang>
Output when using the default (no input):
<pre style="height:30ex;overflow:scroll">
sq¬cubes 21 529
sq¬cubes 21 529
sq¬cubes 22 576
sq¬cubes 22 576
Line 1,497: Line 1,486:
sq¬cubes 30 1089
sq¬cubes 30 1089
</pre>
</pre>
Output when using <tt> 10 </tt> for input:
'''output''' when using the following for input: <tt> 10 </tt>
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">

squares 1 1
squares 1 1
squares 2 4
squares 2 4