我一直在尝试将一些 C# 转换为 F#,但效果并不好,因为我对 F# 还比较陌生
这是有效的 C# 片段:
static void doParallelForeach()
{
// The sum of these elements is 40.
int[] input = { 4, 1, 6, 2, 9, 5, 10, 3 };
int sum = 0;
Parallel.ForEach(
input,
() => 0,
(n, loopState, localSum) =>
{
localSum += n;
Console.WriteLine("Thread={0}, n={1}, localSum={2}", Thread.CurrentThread.ManagedThreadId, n, localSum);
return localSum;
},
(localSum) => Interlocked.Add(ref sum, localSum)
);
}
这是给出此编译器错误的 F# 片段:没有重载匹配方法“ForEach”。
let doParallelForeach =
let input:int[] = [|4; 1; 6; 2; 9; 5; 10; 3|]
let sum = 0
Parallel.ForEach
(
input,
(fun ()-> 0),
(fun (n, loopState, localSum) ->
localSum += n
Console.WriteLine("Thread={0}, n={1}, localSum={2}", Thread.CurrentThread.ManagedThreadId, n, localSum);
return localSum),
(fun (localSum) -> Interlocked.Add(ref sum, localSum))
)
有人可以解释我做错了什么吗?