First class environments: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Add Factor)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 17:
 
When all hailstone values dropped to 1, processing stops, and the total number of hailstone steps for each environment is printed.
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Line 566 ⟶ 567:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
The simplest way to create an environment with variables isolated from code in Icon/Unicon is to create instances of records or class objects.
<lang Icon>link printf
 
procedure main()
every put(environment := [], hailenv(1 to 12,0)) # setup environments
printf("Sequences:\n")
while (e := !environment).sequence > 1 do {
every hailstep(!environment)
printf("\n")
}
printf("\nCounts:\n")
every printf("%4d ",(!environment).count)
printf("\n")
end
 
record hailenv(sequence,count)
 
procedure hailstep(env)
printf("%4d ",env.sequence)
if env.sequence ~= 1 then {
env.count +:= 1
if env.sequence % 2 = 0 then env.sequence /:= 2
else env.sequence := 3 * env.sequence + 1
}
end</lang>
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides formatting]
{{out}}
<pre>Sequences:
1 2 3 4 5 6 7 8 9 10 11 12
1 1 10 2 16 3 22 4 28 5 34 6
1 1 5 1 8 10 11 2 14 16 17 3
1 1 16 1 4 5 34 1 7 8 52 10
1 1 8 1 2 16 17 1 22 4 26 5
1 1 4 1 1 8 52 1 11 2 13 16
1 1 2 1 1 4 26 1 34 1 40 8
1 1 1 1 1 2 13 1 17 1 20 4
1 1 1 1 1 1 40 1 52 1 10 2
1 1 1 1 1 1 20 1 26 1 5 1
1 1 1 1 1 1 10 1 13 1 16 1
1 1 1 1 1 1 5 1 40 1 8 1
1 1 1 1 1 1 16 1 20 1 4 1
1 1 1 1 1 1 8 1 10 1 2 1
1 1 1 1 1 1 4 1 5 1 1 1
1 1 1 1 1 1 2 1 16 1 1 1
1 1 1 1 1 1 1 1 8 1 1 1
1 1 1 1 1 1 1 1 4 1 1 1
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>
 
=={{header|Haskell}}==
Line 728 ⟶ 676:
0 1 7 2 5 8 16 3 19 6 14 9 </pre>
 
=={{header|Icon}} and {{header|Unicon}}==
The simplest way to create an environment with variables isolated from code in Icon/Unicon is to create instances of records or class objects.
<lang Icon>link printf
 
procedure main()
every put(environment := [], hailenv(1 to 12,0)) # setup environments
printf("Sequences:\n")
while (e := !environment).sequence > 1 do {
every hailstep(!environment)
printf("\n")
}
printf("\nCounts:\n")
every printf("%4d ",(!environment).count)
printf("\n")
end
 
record hailenv(sequence,count)
 
procedure hailstep(env)
printf("%4d ",env.sequence)
if env.sequence ~= 1 then {
env.count +:= 1
if env.sequence % 2 = 0 then env.sequence /:= 2
else env.sequence := 3 * env.sequence + 1
}
end</lang>
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides formatting]
{{out}}
<pre>Sequences:
1 2 3 4 5 6 7 8 9 10 11 12
1 1 10 2 16 3 22 4 28 5 34 6
1 1 5 1 8 10 11 2 14 16 17 3
1 1 16 1 4 5 34 1 7 8 52 10
1 1 8 1 2 16 17 1 22 4 26 5
1 1 4 1 1 8 52 1 11 2 13 16
1 1 2 1 1 4 26 1 34 1 40 8
1 1 1 1 1 2 13 1 17 1 20 4
1 1 1 1 1 1 40 1 52 1 10 2
1 1 1 1 1 1 20 1 26 1 5 1
1 1 1 1 1 1 10 1 13 1 16 1
1 1 1 1 1 1 5 1 40 1 8 1
1 1 1 1 1 1 16 1 20 1 4 1
1 1 1 1 1 1 8 1 10 1 2 1
1 1 1 1 1 1 4 1 5 1 1 1
1 1 1 1 1 1 2 1 16 1 1 1
1 1 1 1 1 1 1 1 8 1 1 1
1 1 1 1 1 1 1 1 4 1 1 1
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>
 
=={{header|J}}==
Line 959:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
 
 
=={{header|Kotlin}}==
Line 1,220 ⟶ 1,219:
<pre>
1 2 3 4 5 6 7 8 9 10 11 12
1 1 10 2 16 3 22 4 28 5 34 6
1 1 5 1 8 10 11 2 14 16 17 3
1 1 16 1 4 5 34 1 7 8 52 10
1 1 8 1 2 16 17 1 22 4 26 5
1 1 4 1 1 8 52 1 11 2 13 16
1 1 2 1 1 4 26 1 34 1 40 8
1 1 1 1 1 2 13 1 17 1 20 4
1 1 1 1 1 1 40 1 52 1 10 2
1 1 1 1 1 1 20 1 26 1 5 1
1 1 1 1 1 1 10 1 13 1 16 1
1 1 1 1 1 1 5 1 40 1 8 1
1 1 1 1 1 1 16 1 20 1 4 1
1 1 1 1 1 1 8 1 10 1 2 1
1 1 1 1 1 1 4 1 5 1 1 1
1 1 1 1 1 1 2 1 16 1 1 1
1 1 1 1 1 1 1 1 8 1 1 1
1 1 1 1 1 1 1 1 4 1 1 1
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>
 
=={{header|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.
 
<lang perl6>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 'Counts';
 
say [~] map { $_.<count>.fmt("%4s") }, @hailstones;</lang>
 
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10 11 12
1 1 10 2 16 3 22 4 28 5 34 6
1 1 5 1 8 10 11 2 14 16 17 3
Line 1,619 ⟶ 1,568:
1 1 1 1 1 1 1 1 1 1 1 1
================================================
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
 
=={{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.
 
<lang perl6>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 'Counts';
 
say [~] map { $_.<count>.fmt("%4s") }, @hailstones;</lang>
 
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10 11 12
1 1 10 2 16 3 22 4 28 5 34 6
1 1 5 1 8 10 11 2 14 16 17 3
1 1 16 1 4 5 34 1 7 8 52 10
1 1 8 1 2 16 17 1 22 4 26 5
1 1 4 1 1 8 52 1 11 2 13 16
1 1 2 1 1 4 26 1 34 1 40 8
1 1 1 1 1 2 13 1 17 1 20 4
1 1 1 1 1 1 40 1 52 1 10 2
1 1 1 1 1 1 20 1 26 1 5 1
1 1 1 1 1 1 10 1 13 1 16 1
1 1 1 1 1 1 5 1 40 1 8 1
1 1 1 1 1 1 16 1 20 1 4 1
1 1 1 1 1 1 8 1 10 1 2 1
1 1 1 1 1 1 4 1 5 1 1 1
1 1 1 1 1 1 2 1 16 1 1 1
1 1 1 1 1 1 1 1 8 1 1 1
1 1 1 1 1 1 1 1 4 1 1 1
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>
10,327

edits