Is F # internally running code in parallel by default?

I have a simple question regarding parallel processing in F #. Below is my code for calculating the sum of 1 ^ 2 + 2 ^ 2 + ... + 100 ^ 2 (which is a pretty simple problem, really). As I understand it, the program creates a second list (by the square of each item in the first list and puts them in the second list), then it takes the sum of all the items in the second list. Does the program internally square every item in the original list in parallel? Or is the process just sequential? If so, how can I get the code to run in parallel?

let mySum = 
[1..100]
|>Seq.map (fun n -> n*n)
|>Seq.sum

      

+3


source to share


1 answer


No F # makes code run in parallel by default. Also, by definition, a list is a data structure that has sequential access to its members. You can run your code in parallel using, for example, the F # Array.Parallel module:

let myParSum = 
    [|1..1000|]
    |> Array.Parallel.map (fun n -> n * n)
    |> Array.sum

      



or use the standard .NET parallel task library .

+6


source







All Articles