First class environments: Difference between revisions

Added FreeBASIC
m (→‎{{header|Raku}}: remove what's not needed)
(Added FreeBASIC)
 
(2 intermediate revisions by 2 users not shown)
Line 538:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
 
=={{header|FreeBASIC}}==
{{trans|Wren}}
<syntaxhighlight lang="vbnet">Type E
As Integer _valor, _contar
Public:
Declare Sub Constructor_(value As Integer, count As Integer)
Declare Function Valor() As Integer
Declare Function Contar() As Integer
Declare Sub Hailstone()
End Type
 
Sub E.Constructor_(value As Integer, count As Integer)
This._valor = value
This._contar = count
End Sub
 
Function E.Valor() As Integer
Return This._valor
End Function
 
Function E.Contar() As Integer
Return This._contar
End Function
 
Sub E.Hailstone()
Print Using "####"; This._valor;
If (This._valor = 1) Then Exit Sub
This._contar = This._contar + 1
This._valor = Iif(This._valor Mod 2 = 0, This._valor \ 2, 3 * This._valor + 1)
End Sub
 
 
Dim As Integer jobs = 12
Dim As E envs(jobs)
 
For i As Integer = 0 To jobs - 1
envs(i).Constructor_(i + 1, 0)
Next i
 
Print "Sequences:"
Dim As Integer done = 0
While done = 0
For i As Integer = 0 To jobs - 1
envs(i).Hailstone()
Next i
Print
done = 1
For i As Integer = 0 To jobs - 1
If envs(i).Valor() <> 1 Then
done = 0
Exit For
End If
Next i
Wend
 
Print "Counts:"
For i As Integer = 0 To jobs - 1
Print Using "####"; envs(i).Contar();
Next i
Print
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Wren entry.</pre>
 
=={{header|Go}}==
Line 851 ⟶ 917:
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}}==
Line 2,176 ⟶ 2,347:
 
So that's what we use here. However, to create a dynamic class you have to wrap it in a function which then returns a reference to the class object.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var environment = Fn.new {
2,122

edits