F#在Seq <'T>中求和多个字段

Mat*_*ews 5 f# seq

我希望能够对序列的多个字段求和.我不知道如何在不重复迭代序列的情况下执行此操作.我的看法是效率低下.有没有一种聪明的方法可以做到这一点,我没有看到?

type item = {
    Name : string
    DemandQty : decimal
    Weight : decimal
    UnitCost : decimal
}

let items =
    {1..10}
    |> Seq.map (fun x -> 
        {
            Name = string x
            DemandQty = decimal x
            Weight = decimal x
            UnitCost = decimal x
        })

// This Works for a single value
let costSum =
    items
    |> Seq.sumBy (fun x -> x.DemandQty * x.UnitCost)

// This is similar to what I would like to do
let costSum, weightSum =
    items
    |> Seq.sumBy (fun x -> x.DemandQty * x.UnitCost, x.DemandQty * x.Weight)
Run Code Online (Sandbox Code Playgroud)

理想情况下,我可以通过映射序列来计算我正在计算的多个值.我的想法是在这里吗?

如果我必须多次迭代序列来计算我想要的所有总和,我也会对知道性能影响是多少感兴趣.我的看法是效率低下.

小智 6

一种直接的求和方法是使用求和函数折叠序列:

let costSum, weightSum =
    items
    |> Seq.fold 
        (fun (costSum, weightSum) x -> (costSum + x.DemandQty * x.UnitCost, weightSum + x.DemandQty * x.Weight)) 
        (0m, 0m)
Run Code Online (Sandbox Code Playgroud)

对于迭代序列的性能影响,多次是依赖于它.重复迭代序列的工作.因此,面对它效率较低.然而,对于较短的序列,当重复迭代次数不变时,性能影响可以忽略不计.计算复杂性理论还指出,当元素数量增加时,常数是可忽略的.

简而言之,如果重要,则以预期投入为基准.如果它没有足够大的影响,请使用提供最佳清晰度的解决方案.