Generator/Exponential: Difference between revisions
Content added Content deleted
(add scala) |
m (→{{header|REXX}}: deleted a superflous blank lines. -- ~~~~) |
||
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>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).*/▼
▲/*REXX program to show how to use a generator (also known as iterators).*/
numeric digits 10000 /*just in case we need big 'uns. */
gen_.= /*have generators from scratch. */
/*how_many: show that many vals.*/
parse arg how_many; how_many=pick1(how_many 0)
do j=1
do j=1
▲ 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=20+1
exit
/*─────────────────────────────────────generate powers iterator─────────*/
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
return gen_.powers.x.p
/*─────────────────────────────────────gen squares iterator─────────────*/
gen_squares: procedure expose gen_.; parse arg x;if x=='' then return
Line 1,449 ⟶ 1,443:
end
return gen_.squares.x
/*─────────────────────────────────────gen cubes iterator───────────────*/
gen_cubes: procedure expose gen_.; parse arg x; if x=='' then return
Line 1,457 ⟶ 1,450:
end
return gen_.cubes.x
/*─────────────────────────────────────gen squares not cubes iterator───*/
gen_sqNcubes: procedure expose gen_.; parse arg x; if x=='' then return
Line 1,477 ⟶ 1,469:
end /*j*/
return gen_.sqNcubes.x
/*─────────────────────────────────────"1─liner" subroutines────────────*/
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
pick1: return word(arg(1) arg(2),1)</lang>▼
▲pick1: return word(arg(1) arg(2),1)
▲Output when using the default (no input):
▲<pre style="height:30ex;overflow:scroll">
sq¬cubes 21 529
sq¬cubes 22 576
Line 1,497 ⟶ 1,486:
sq¬cubes 30 1089
</pre>
<pre style="height:30ex;overflow:scroll">
squares 1 1
squares 2 4
|