Loops/With multiple ranges: Difference between revisions

Content deleted Content added
Flagged for clarification.
Thundergnat (talk | contribs)
m →‎{{header|Perl 6}}: Combine code blocks to make complete, runnable examples, twiddle whitespace
Line 288: Line 288:
Also displaying the j sequence since it isn't very large.
Also displaying the j sequence since it isn't very large.


<lang perl6>sub comma ($i) {
<lang perl6>sub comma ($i) {
my $sign = $i < 0 ?? '-' !! '';
my $sign = $i < 0 ?? '-' !! '';
$sign ~ $i.abs.flip.comb(3).join(',').flip
$sign ~ $i.abs.flip.comb(3).join(',').flip
Line 301: Line 301:


my $j = flat
my $j = flat
(-three, *+three … 3³),
( -three, *+three … ),
(-seven, *+x …^ * > seven),
( -seven, *+x …^ * > seven ),
(555 .. 550 - y),
( 555 .. 550 - y ),
(22, *-three …^ * < -28),
( 22, *-three …^ * < -28 ),
(1927 .. 1939),
( 1927 .. 1939 ),
(x, *+z …^ * < y),
( x, *+z …^ * < y ),
(11**x .. 11**x + one);
( 11**x .. 11**x + one );


put 'j sequence: ', $j;
put 'j sequence: ', $j;
put ' Sum: ', comma [+] $j».abs;
put ' Sum: ', comma [+] $j».abs;
put ' Product: ', comma ([\*] $j.grep: so *).first: *.abs > 2²⁷;</lang>
put ' Product: ', comma ([\*] $j.grep: so +*).first: *.abs > 2²⁷;

{{Out}}
# Or, an alternate method for generating the 'j' sequence, employing user-defined
<pre>j sequence: -3 0 3 6 9 12 15 18 21 24 27 -7 -2 3 555 22 19 16 13 10 7 4 1 -2 -5 -8 -11 -14 -17 -20 -23 -26 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 5 3 1 -1 -3 -5 161051 161052
# operators to more closely mimic the 'X to Y by Z' layout of the example code.
Sum: 348,173

Product: -793,618,560</pre>
sub infix:<to> { $^a...$^b }
==== Literal-minded variation ====
An alternate method for generating the 'j' sequence, employing user-defined operators to preserve the 'X to Y by Z' layout of the example code.
<lang perl6>sub infix:<to> { $^a...$^b }
sub infix:<by> { ($^a.split(' '))[0,$^b.abs ... *] }
sub infix:<by> { ($^a.split(' '))[0,$^b.abs ... *] }


$j = flat
$j = cache flat
-three to 3**3 by three ,
-three to 3**3 by three,
-seven to seven by x ,
-seven to seven by x ,
555 to (550 - y) ,
555 to (550 - y) ,
Line 328: Line 326:
1927 to 1939 by one ,
1927 to 1939 by one ,
x to y by z ,
x to y by z ,
11**x to (11**x+one);</lang>
11**x to (11**x + one);

put "\nLiteral minded variant:";
put ' Sum: ', comma [+] $j».abs;
put ' Product: ', comma ([\*] $j.grep: so +*).first: *.abs > 2²⁷;</lang>
{{Out}}
<pre>j sequence: -3 0 3 6 9 12 15 18 21 24 27 -7 -2 3 555 22 19 16 13 10 7 4 1 -2 -5 -8 -11 -14 -17 -20 -23 -26 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 5 3 1 -1 -3 -5 161051 161052
Sum: 348,173
Product: -793,618,560

Literal minded variant:
Sum: 348,173
Product: -793,618,560
</pre>


=={{header|REXX}}==
=={{header|REXX}}==