Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are lots of problems with your code. Here's a literal translation:</p> <pre><code>let doParallelForeach() = let input:int[] = [|4; 1; 6; 2; 9; 5; 10; 3|] let sum = ref 0 Parallel.ForEach ( input, (fun ()-&gt; 0), (fun n loopState localSum-&gt; let localSum = localSum + n Console.WriteLine("Thread={0}, n={1}, localSum={2}", Thread.CurrentThread.ManagedThreadId, n, localSum) localSum), (fun (localSum) -&gt; Interlocked.Add(sum, localSum) |&gt; ignore)) </code></pre> <p>Notes:</p> <ol> <li>Since your C# code uses a method, I've made <code>doParallelForeach</code> into a function instead of a value of type <code>unit</code>.</li> <li><code>return</code> and <code>+=</code> aren't valid F# code. You don't need to use <code>return</code> in F#, and you can only modify mutable values (and even then, there are no compound assignment operators). Instead, we just shadow <code>localSum</code> with a new value.</li> <li>You're using <code>ref</code> incorrectly - it is a function in F#, not a modifier, and it is used to define new reference values, not to extract addresses to pass to methods. Furthermore, since <code>sum</code> was a let-bound immutable value, you couldn't take its address anyway. The correct way to do this is to use F#'s <code>ref</code> function to make <code>sum</code> an instance of the <code>int ref</code> type.</li> <li>In F#, you use curried (as opposed to tupled) function definitions when defining a delegate. That is, you use <code>fun n loopState localSum -&gt; ...</code> instead of <code>fun (n, loopState, localSum) -&gt; ...</code>.</li> </ol> <p>Note that regardless of these fixes, this is a weird method - the results are kept in a local variable (<code>sum</code>) which isn't ever exposed to the caller...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload