Intersecting number wheels: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 61:
 
<br>
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN
Line 289 ⟶ 290:
1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
 
public static class IntersectingNumberWheels
{
public static void Main() {
TurnWheels(('A', "123")).Take(20).Print();
TurnWheels(('A', "1B2"), ('B', "34")).Take(20).Print();
TurnWheels(('A', "1DD"), ('D', "678")).Take(20).Print();
TurnWheels(('A', "1BC"), ('B', "34"), ('C', "5B")).Take(20).Print();
}
 
static IEnumerable<char> TurnWheels(params (char name, string values)[] wheels) {
var data = wheels.ToDictionary(wheel => wheel.name, wheel => wheel.values.Loop().GetEnumerator());
var primary = data[wheels[0].name];
while (true) {
yield return Turn(primary);
}
 
char Turn(IEnumerator<char> sequence) {
sequence.MoveNext();
char c = sequence.Current;
return char.IsDigit(c) ? c : Turn(data[c]);
}
}
 
static IEnumerable<T> Loop<T>(this IEnumerable<T> seq) {
while (true) {
foreach (T element in seq) yield return element;
}
}
 
static void Print(this IEnumerable<char> sequence) => Console.WriteLine(string.Join(" ", sequence));
}</lang>
{{out}}
<pre>
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
1 3 2 1 4 2 1 3 2 1 4 2 1 3 2 1 4 2 1 3
1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4</pre>
 
=={{header|C++}}==
Line 421 ⟶ 465:
1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
 
public static class IntersectingNumberWheels
{
public static void Main() {
TurnWheels(('A', "123")).Take(20).Print();
TurnWheels(('A', "1B2"), ('B', "34")).Take(20).Print();
TurnWheels(('A', "1DD"), ('D', "678")).Take(20).Print();
TurnWheels(('A', "1BC"), ('B', "34"), ('C', "5B")).Take(20).Print();
}
 
static IEnumerable<char> TurnWheels(params (char name, string values)[] wheels) {
var data = wheels.ToDictionary(wheel => wheel.name, wheel => wheel.values.Loop().GetEnumerator());
var primary = data[wheels[0].name];
while (true) {
yield return Turn(primary);
}
 
char Turn(IEnumerator<char> sequence) {
sequence.MoveNext();
char c = sequence.Current;
return char.IsDigit(c) ? c : Turn(data[c]);
}
}
 
static IEnumerable<T> Loop<T>(this IEnumerable<T> seq) {
while (true) {
foreach (T element in seq) yield return element;
}
}
 
static void Print(this IEnumerable<char> sequence) => Console.WriteLine(string.Join(" ", sequence));
}</lang>
{{out}}
<pre>
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
1 3 2 1 4 2 1 3 2 1 4 2 1 3 2 1 4 2 1 3
1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4</pre>
 
=={{header|D}}==
Line 896 ⟶ 897:
[('A',"1DD"),('D',"678")]
[('A',"1BC"),('B',"34"),('C',"5B")]</pre>
 
=={{header|Java}}==
<lang Java>
Line 993 ⟶ 995:
{A=[1, B, C], B=[3, 4], C=[5, B]}
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4
 
 
=={{header|JavaScript}}==
Line 1,428 ⟶ 1,429:
C: 5, B
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4</pre>
 
=={{header|Perl 6}}==
A succinct Perl 6 example using a few additional language features. Wheels are implemented as infinite repeating sequences, allowing a single iterator to keep track of the current position. This means the code contains no position tracking whatsoever.
<lang perl6>
#| advance rotates a named wheel $n by consuming an item from an infinite sequence. It is called
#| from within a gather block and so can use take in order to construct an infinite, lazy sequence
#| of result values
sub advance($g, $n) {
given $g{$n}.pull-one {
when /\d/ { take $_ }
default { samewith $g, $_ } # samewith re-calls this function with new parameters
}
}
 
#| Input groups are a hash containing each wheel name as the key, and a list of values constructed
#| using <> to split on whitespace. They are transformed using xx * to repeat the list infinitely.
#| We then retrieve the underlying iterator in order for wheel position to be persistent. Each group
#| is then aggregated into a lazy output sequence using an infinite loop inside a gather block.
[
{A => <1 2 3>},
{A => <1 B 2>, B => <3 4>},
{A => <1 D D>, D => <6 7 8>},
{A => <1 B C>, B => <3 4>, C => <5 B>},
]
#| %() converts a list of pairs produced by map into a hash. $^k and $^v are implicit variables.
#| They are processed in alphabetical order and make the block arity 2, called with two vars.
#| .kv gets the list of wheel names and wheel values from the input entry
==> map({ %(.kv.map: { $^k => (|$^v xx *).iterator }) })
#| gather constructs a lazy sequence, in which we infinitely loop advancing wheel A
==> map({ gather { loop { advance $_, 'A' }} })
#| state variables are only initialised once, and are kept between invocations.
==> map({ state $i = 1; say "Group {$i++}, First 20 values: $_[^20]" })
</lang>{{Output}}
<pre>
Group 1, First 20 values: 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
Group 2, First 20 values: 1 3 2 1 4 2 1 3 2 1 4 2 1 3 2 1 4 2 1 3
Group 3, First 20 values: 1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6
Group 4, First 20 values: 1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4
</pre>
 
=={{header|Phix}}==
Line 1,798 ⟶ 1,760:
{'A': '1DD', 'D': '678'}
{'A': '1BC', 'B': '34', 'C': '5B'}</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
A succinct Perl 6 example using a few additional language features. Wheels are implemented as infinite repeating sequences, allowing a single iterator to keep track of the current position. This means the code contains no position tracking whatsoever.
<lang perl6>
#| advance rotates a named wheel $n by consuming an item from an infinite sequence. It is called
#| from within a gather block and so can use take in order to construct an infinite, lazy sequence
#| of result values
sub advance($g, $n) {
given $g{$n}.pull-one {
when /\d/ { take $_ }
default { samewith $g, $_ } # samewith re-calls this function with new parameters
}
}
 
#| Input groups are a hash containing each wheel name as the key, and a list of values constructed
#| using <> to split on whitespace. They are transformed using xx * to repeat the list infinitely.
#| We then retrieve the underlying iterator in order for wheel position to be persistent. Each group
#| is then aggregated into a lazy output sequence using an infinite loop inside a gather block.
[
{A => <1 2 3>},
{A => <1 B 2>, B => <3 4>},
{A => <1 D D>, D => <6 7 8>},
{A => <1 B C>, B => <3 4>, C => <5 B>},
]
#| %() converts a list of pairs produced by map into a hash. $^k and $^v are implicit variables.
#| They are processed in alphabetical order and make the block arity 2, called with two vars.
#| .kv gets the list of wheel names and wheel values from the input entry
==> map({ %(.kv.map: { $^k => (|$^v xx *).iterator }) })
#| gather constructs a lazy sequence, in which we infinitely loop advancing wheel A
==> map({ gather { loop { advance $_, 'A' }} })
#| state variables are only initialised once, and are kept between invocations.
==> map({ state $i = 1; say "Group {$i++}, First 20 values: $_[^20]" })
</lang>{{Output}}
<pre>
Group 1, First 20 values: 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
Group 2, First 20 values: 1 3 2 1 4 2 1 3 2 1 4 2 1 3 2 1 4 2 1 3
Group 3, First 20 values: 1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6
Group 4, First 20 values: 1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4
</pre>
 
=={{header|REXX}}==
10,327

edits