Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# how to do List.map in parallel

What is the most lightweight, terse way to run the following code in parallel within the standard F# libs? Or failing that any widely used additional libs?

let newlist = oldlist |> List.map myComplexFunction

The best I could find was

let newlist = oldlist |> List.map (fun x -> async { return myComplexFunction x }
                      |> Async.Parallel 
                      |> Async.RunSynchronously
                      |> Array.toList

I don't like this because it's 4 lines long and constructs an array that I then have to make back into a list. If I were working with Arrays it would be simple, Array.parallel, but I want to keep that lovely Immutable list functional purity. I just can't believe there is no list alternative, but so far have been unable to find one. Any good suggestions?

like image 360
Daniel Slater Avatar asked Dec 04 '25 16:12

Daniel Slater


1 Answers

Use the PSeq module:

open Microsoft.FSharp.Collections

let newlist = 
    oldlist 
    |> PSeq.map myComplexFunction
    |> PSeq.toList
like image 79
Daniel Avatar answered Dec 06 '25 07:12

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!