我正在研究我在 C# 中解决的问题的解决方案,但现在我正在将我的代码移植到 F#。我有一个字节数组,我需要在这个数组中搜索两个值 (x, y)。当我找到 x
,需要检查:如果y
在下一个索引上,我们有一个匹配。如果 y
不在下一个索引上,搜索将继续。我应该如何解决这个问题?我试过使用 Array.findIndex
,但没有成功,因为我不知道如何在 y
时继续搜索不在下一个索引上。
编辑:
public void GetValue(byte[] data)
{
byte[] temp = new byte[4];
for (int i = 0; i < data.Length; i++)
{
if (data[i] == Adress[0] && data[i + 1] == Adress[1])
{
for (int j = 0; j < temp.Length; j++)
{
temp[j] = data[j + i + 2];
}
Value = BitConverter.ToInt32(temp, 0) * 0.01;
break;
}
}
最佳答案
你可以很容易地把它变成一个函数:
let bytes = [| 104uy; 101uy; 108uy; 108uy; 111uy |]
bytes
|> Array.pairwise
|> Array.findIndex (fun x -> fst x = snd x)
//2
关于f# - 在数组中搜索 2 个相邻的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45454743/