【发布时间】:2018-06-14 21:25:21
【问题描述】:
我希望能够对序列的多个字段求和。如果不多次迭代序列,我不知道如何做到这一点。我的看法是,这是低效的。有没有我看不到的聪明方法?
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)
理想情况下,我可以通过在序列上映射来对我正在计算的多个值求和。我的想法在这里吗?
如果我必须多次遍历序列来计算我想要的所有总和,我也会有兴趣了解性能影响。我的看法是这样做效率低下。
【问题讨论】: