First class environments: Difference between revisions

m
→‎{{header|Raku}}: remove what's not needed
m (syntax highlighting fixup automation)
m (→‎{{header|Raku}}: remove what's not needed)
Line 1,724:
=={{header|Raku}}==
(formerly Perl 6)
 
{{Works with|rakudo|2015-12-17}}
Fairly straightforward. Set up an array of hashes containing the current values and iteration counts then pass each hash in turn with a code reference to a routine to calculate the next iteration.
 
<syntaxhighlight lang="raku" line>my $calculator = sub ($n is rw) {
return ($n == 1) ?? 1 !! $n %% 2 ?? $n div 2 !! $n * 3 + 1
};
 
sub next (%this, &get_next) {
return %this if %this.<value> == 1;
%this.<value> .= &get_next;
%this.<count>++;
return %this;
};
 
my @hailstones = map { %(value => $_, count => 0) }, 1 .. 12;
 
while not all( map { $_.<value> }, @hailstones ) == 1 {
say [~] map { $_.<value>.fmt(": '%4s")' }, @hailstones;
@hailstones[$_] .= &next($calculator) for ^@hailstones;
}
 
say "\nCounts\n" ~ [~] map { $_.<count>.fmt(": '%4s")' }, @hailstones;</syntaxhighlight>
say 'Counts';
 
say [~] map { $_.<count>.fmt("%4s") }, @hailstones;</syntaxhighlight>
 
{{out}}
Line 1,770 ⟶ 1,768:
1 1 1 1 1 1 1 1 2 1 1 1
Counts
0 1 7 2 5 8 16 3 19 6 14 9</pre>
</pre>
 
=={{header|REXX}}==
The formatting is sensitive to a terminating Collatz sequence and is shown as blanks &nbsp; (that is,
2,392

edits