First class environments: Difference between revisions

New post.
m (→‎{{header|Raku}}: remove what's not needed)
(New post.)
Line 851:
0 1 7 2 5 8 16 3 19 6 14 9</syntaxhighlight>
In essence: run is a static method of the class <code>hailstone</code> which, given a list of objects of the class runs all of them until their hailstone sequence number stops changing. It also displays the hailstone sequence number from each of the objects at each step. Its result is the step count from each object.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
public final class FirstClassEnvironments {
 
public static void main(String[] aArgs) {
code();
}
private static void code() {
do {
for ( int job = 0; job < JOBS; job++ ) {
switchTo(job);
hailstone();
}
System.out.println();
} while ( ! allDone() );
 
System.out.println(System.lineSeparator() + "Counts:");
for ( int job = 0; job < JOBS; job++ ) {
switchTo(job);
System.out.print(String.format("%4d", count));
}
System.out.println();
}
private static boolean allDone() {
for ( int job = 0; job < JOBS; job++ ) {
switchTo(job);
if ( sequence > 1 ) {
return false;
}
}
return true;
}
private static void hailstone() {
System.out.print(String.format("%4d", sequence));
if ( sequence == 1 ) {
return;
}
count += 1;
sequence = ( sequence % 2 == 1 ) ? 3 * sequence + 1 : sequence / 2;
}
private static void switchTo(int aID) {
if ( aID != currentId ) {
environments.get(currentId).seq = sequence;
environments.get(currentId).count = count;
currentId = aID;
}
sequence = environments.get(aID).seq;
count = environments.get(aID).count;
}
private static class Environment {
public Environment(int aSeq, int aCount) {
seq = aSeq; count = aCount;
}
private int seq, count;
}
private static int sequence, count, currentId;
private static List<Environment> environments =
IntStream.rangeClosed(1, 12).mapToObj( i -> new Environment(i, 0 ) ).collect(Collectors.toList());
private static final int JOBS = 12;
 
}
</syntaxhighlight>
{{ 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>
 
=={{header|jq}}==
871

edits