F# 对 Seq<'T> 中的多个字段求和
F# Summing multiple fields in Seq<'T>
我希望能够对一个序列的多个字段求和。如果不多次迭代序列,我不知道如何做到这一点。我的看法是,这是低效的。有没有我没有看到的聪明方法?
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)
理想情况下,我可以通过映射序列计算多个值的总和。我的想法是不是在这里?
我也想知道如果我必须多次遍历序列以计算我想要的所有总和,会对性能产生什么影响。我的看法是这样效率不高。
一种简单的求和方法是使用求和函数折叠序列:
let costSum, weightSum =
items
|> Seq.fold
(fun (costSum, weightSum) x -> (costSum + x.DemandQty * x.UnitCost, weightSum + x.DemandQty * x.Weight))
(0m, 0m)
至于多次迭代序列对性能的影响是视情况而定。重复遍历序列的工作。所以从表面上看效率较低。然而,对于较短的序列,当重复迭代次数恒定时,性能影响可能可以忽略不计。计算复杂性理论还指出,当元素数量增加时,常数是可以忽略的。
简而言之,如果重要的话,请以预期输入为基准。如果它没有足够大的影响,请使用提供最佳清晰度的解决方案。
我希望能够对一个序列的多个字段求和。如果不多次迭代序列,我不知道如何做到这一点。我的看法是,这是低效的。有没有我没有看到的聪明方法?
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)
理想情况下,我可以通过映射序列计算多个值的总和。我的想法是不是在这里?
我也想知道如果我必须多次遍历序列以计算我想要的所有总和,会对性能产生什么影响。我的看法是这样效率不高。
一种简单的求和方法是使用求和函数折叠序列:
let costSum, weightSum =
items
|> Seq.fold
(fun (costSum, weightSum) x -> (costSum + x.DemandQty * x.UnitCost, weightSum + x.DemandQty * x.Weight))
(0m, 0m)
至于多次迭代序列对性能的影响是视情况而定。重复遍历序列的工作。所以从表面上看效率较低。然而,对于较短的序列,当重复迭代次数恒定时,性能影响可能可以忽略不计。计算复杂性理论还指出,当元素数量增加时,常数是可以忽略的。
简而言之,如果重要的话,请以预期输入为基准。如果它没有足够大的影响,请使用提供最佳清晰度的解决方案。