我有一个代码在C#中生成序列,该序列通常枚举所有可能的有序排列,如下所示:
/// foreach (int[] row in Sequences(new[] { 2, 2 }))
/// {
/// // The following sequences will be generated in order:
/// //
/// // new int[] { 0, 0, 0 },
/// // new int[] { 0, 0, 1 },
/// // new int[] { 0, 1, 0 },
/// // new int[] { 0, 1, 1 },
/// // new int[] { 1, 0, 0 },
/// // new int[] { 1, 0, 1 },
/// // new int[] { 1, 1, 0 },
/// // new int[] { 1, 1, 1 },
/// }
public static IEnumerable<int[]> Sequences(this int[] symbols, bool inPlace = false)
{
var current = new int[symbols.Length];
for (int i = 0; i < symbols.Length; i++)
{
if (symbols[i] == 0)
yield break;
}
while (true)
{
yield return inPlace ? current : (int[])current.Clone();
for (int j = symbols.Length - 1; j >= 0; j--)
{
if (current[j] != symbols[j] - 1)
{
current[j]++;
break;
}
if (j == 0)
yield break;
current[j] = 0;
}
}
}
我想知道Go是否有等效功能(因为直到v2才真正存在泛型)。