Intersecting number wheels: Difference between revisions

Added C#
m (→‎{{header|REXX}}: changed come whitespace and comments, used more appropriate index names for do loops.)
(Added C#)
Line 141:
 
</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|F_Sharp|F#}}==
196

edits