F# 如何并行执行 List.map

F# how to do List.map in parallel

在标准 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 行长,并且构建了一个数组,然后我必须将其重新放入列表中。如果我使用数组,它会很简单,Array.parallel,但我想保持可爱的 Immutable 列表功能纯度。我简直不敢相信没有列表替代品,但到目前为止还找不到。 有什么好的建议吗?

使用PSeq模块:

open Microsoft.FSharp.Collections

let newlist = 
    oldlist 
    |> PSeq.map myComplexFunction
    |> PSeq.toList

现在值得一看 F# Core 中的 Array.Parallel

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