Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run Seq.iter's action function asynchronously

So, the title describes it all.

Seq.iter (fun item -> (*do something*)) sequence

Can I run all those lambda-functions as parallel? It can be useful when this function does some big work.

like image 697
Kirill Dubovikov Avatar asked Jul 20 '26 12:07

Kirill Dubovikov


2 Answers

Sure, if you are targeting .NET 3.5 +, then

  1. Download the F# PowerPack
  2. Add a reference to FSharp.PowerPack.Parallel.Seq
  3. open Microsoft.FSharp.Collections
  4. Code away using PSeq module, e.g. {1..100} |> PSeq.iter (printfn "%A")

If you are targeting .NET 2.0, follow my instructions here to compile a back-ported version of System.Linq.ParallelEnumerable from the Rx project into the PowerPack Parallel.Seq project.

like image 159
Stephen Swensen Avatar answered Jul 23 '26 09:07

Stephen Swensen


If you want to run the function in parallel just use the Parallel.ForEach function

Parallel.ForEach(sequence, fun item -> (* do something *))

This works just fine from F#

like image 40
JaredPar Avatar answered Jul 23 '26 09:07

JaredPar



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!