First class environments: Difference between revisions

Content added Content deleted
(→‎{{header|jq}}: space vs time)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, used output templates, reduced font size for 2nd output, simplified code.)
Line 1,318: Line 1,318:


=={{header|REXX}}==
=={{header|REXX}}==
The formatting is sensative to a terminating Collatz sequence and is shown as blanks.
The formatting is sensitive to a terminating Collatz sequence and is shown as blanks   (that is,
<br>Column widths are automatically adjusted for their width (maximum number displayed in a column).
<br>once a &nbsp; '''1''' &nbsp; (unity) &nbsp; is found, no more numbers are displayed in that column).

<br>The '''hailstone''' function (subroutine) could be coded in-line to further comply with the task's requirement that
Column widths are automatically adjusted for their width (maximum number displayed in a column).

The '''hailstone''' function (subroutine) could be coded in-line to further comply with the task's requirement that
<br>the solution have a &nbsp; ''single piece of code to be run repeatedly in each of these environments''.
<br>the solution have a &nbsp; ''single piece of code to be run repeatedly in each of these environments''.
<lang rexx>/*REXX program illustrates first-class environments (using hailstone #s)*/
<lang rexx>/*REXX program illustrates 1st─class environments (using the numbers from hailstone seq)*/
parse arg N .; if N=='' then N=12 /*Was N defined? No, use default.*/
parse arg N . /*obtain optional argument from the CL.*/
w=length(N) /*the width (so far) for columns.*/
if N=='' | N=="," then N=12 /*Was N defined? No, then use default.*/
w=length(N) /*width (so far) for columnar output.*/
@.=
@.=
do init=1 for N; @.init=init; end /*initialize all the environments*/
do i=1 for N; @.i=i; end /*i*/ /*initialize all the environments. */


do forever until @.0; @.0=1 /* ◄─── process all environments.*/
do forever until @.0; @.0=1 /* ◄─── process all the environments. */
do k=1 for N; x=hailstone(k); w=max(w,length(x))
do k=1 for N; x=hailstone(k) /*obtain next hailstone number in seq. */
@.k=@.k x /*where the rubber meets the road*/
w=max(w, length(x) ) /*determine the maximum width needed. */
@.k=@.k x /* ◄─── where the rubber meets the road*/
end /*k*/
end /*forever*/
end /*k*/
end /*forever*/

count=0 /* [↓] show tabular results. */
#=0 /* [↓] display the tabular results. */
do lines=-1 until _==''; _= /*process each line for each env.*/
do lines=-1 until _=''; _= /*process a line for each environment. */
do j=1 for N /*process each environment. */
do j=1 for N /*process each of the environments. */
select /*choose how to process the line.*/
select /*determine how to process the line. */
when count== 1 then _=_ right(words(@.j)-1, w)
when #== 1 then _=_ right(words(@.j) - 1, w)
when lines==-1 then _=_ right(j, w) /*hdr.*/
when lines==-1 then _=_ right(j, w) /*the header. */
when lines== 0 then _=_ right('', w, '') /*sep.*/
when lines== 0 then _=_ right('', w, "") /*the separator. */
otherwise _=_ right(word(@.j, lines), w)
otherwise _=_ right(word(@.j, lines), w)
end /*select*/
end /*select*/
end /*j*/
end /*j*/
if #==1 then #=2

if _='' then #=# + 1 /*Null? Bump #. */
if count==1 then count=2
_=strip(_,'T'); if _=='' then count=count+1 /*if null, bump*/
if #==1 then _=copies(" "left('', w, "═"), N) /*foot separator.*/
if count==1 then _=copies(" "left('', w, ""), N) /*foot sep*/
if _\='' then say strip( substr(_, 2), "T") /*display counts.*/
end /*lines*/
if _\=='' then say substr(_,2) /*show the (foot) counts.*/
end /*lines*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
exit /*stick a fork in it, we're done.*/
hailstone: procedure expose @.; parse arg y; _=word(@.y, words(@.y) )
/*─────────────────────────────────────HAILSTONE (Collatz) subroutine───*/
if _==1 then return ''; @.0=0; if _//2==0 then return _%2; return _*3+1</lang>
hailstone: procedure expose @.; parse arg y; _=word(@.y, words(@.y))
{{out|output|text=&nbsp; when using the default input:}}
if _==1 then return '' ; @.0=0; if _//2==0 then return _%2; return _*3+1</lang>
'''output''' using the default input:
<pre>
<pre>
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
── ── ── ── ── ── ── ── ── ── ── ──
─── ─── ─── ─── ─── ─── ─── ─── ─── ─── ─── ───
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
1 10 2 16 3 22 4 28 5 34 6
1 10 2 16 3 22 4 28 5 34 6
5 1 8 10 11 2 14 16 17 3
5 1 8 10 11 2 14 16 17 3
16 4 5 34 1 7 8 52 10
16 4 5 34 1 7 8 52 10
8 2 16 17 22 4 26 5
8 2 16 17 22 4 26 5
4 1 8 52 11 2 13 16
4 1 8 52 11 2 13 16
2 4 26 34 1 40 8
2 4 26 34 1 40 8
1 2 13 17 20 4
1 2 13 17 20 4
1 40 52 10 2
1 40 52 10 2
20 26 5 1
20 26 5 1
10 13 16
10 13 16
5 40 8
5 40 8
16 20 4
16 20 4
8 10 2
8 10 2
4 5 1
4 5 1
2 16
2 16
1 8
1 8
4
4
2
2
1
1
══ ══ ══ ══ ══ ══ ══ ══ ══ ══ ══ ══
═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══
0 1 7 2 5 8 16 3 19 6 14 9
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
</pre>
'''output''' when using the input of: &nbsp; <tt> 60 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 60 </tt>}}

<pre style="height:90ex;overflow:scroll">
(Shown at three-quarter size.)
<pre style="font-size:75%;height:115ex">
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ────
──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ────
Line 1,500: Line 1,505:
════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════
════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════
0 1 7 2 5 8 16 3 19 6 14 9 9 17 17 4 12 20 20 7 7 15 15 10 23 10 111 18 18 18 106 5 26 13 13 21 21 21 34 8 109 8 29 16 16 16 104 11 24 24 24 11 11 112 112 19 32 19 32 19
0 1 7 2 5 8 16 3 19 6 14 9 9 17 17 4 12 20 20 7 7 15 15 10 23 10 111 18 18 18 106 5 26 13 13 21 21 21 34 8 109 8 29 16 16 16 104 11 24 24 24 11 11 112 112 19 32 19 32 19

</pre>
</pre>