I am wanting to be able to sum multiple fields of a sequence. I don't know how to do this without iterating over the sequence multiple times. My perception is that this is inefficient. Is there a clever way to do this that I am not seeing?
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)
Ideally I can sum multiple values that I am calculating by mapping over the sequence. Is my thinking off here?
I would also be interested in knowing what the performance impact is going to be if I have to iterate through the sequence multiple times to calculate all of the sums I am wanting. My perception is that this would be inefficient.