f#如何做列表.并行映射



在标准f#库中并行运行以下代码的最轻量级、最简洁的方法是什么?或者没有任何广泛使用的附加库?

let newlist = oldlist |> List.map myComplexFunction

我能找到的最好的是

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

我不喜欢这个,因为它有4行长,构造了一个数组,然后我必须把它变成一个列表。如果我用的是数组,那就很简单了。并行,但我想保持不可变列表函数的纯洁性。我只是不敢相信没有其他选择,但到目前为止我还没有找到。有什么好的建议吗?

使用PSeq模块:

open Microsoft.FSharp.Collections
let newlist = 
    oldlist 
    |> PSeq.map myComplexFunction
    |> PSeq.toList

现在有必要看一下Array。f# Core中的并行。

let newlist =
    oldlist
    |> Array.ofList
    |> Array.Parallel.map (fun x -> myComplexFunction x)
    |> Array.toList

最新更新