I have the following nested calls to Seq.map () in a function that compiles and works efficiently:
|> Seq.map (fun (tradedOnKey : DateTime, portfolioSeq : seq<PortfolioId * seq<PortfolioFundRecord>>) ->
let pricedPortfoliosGroup =
portfolioSeq
|> Seq.map (fun (p : PortfolioId, spsf : (seq<PortfolioFundRecord>)) ->
let price =
spsf
|> Seq.map (fun (pfr : PortfolioFundRecord) -> pfr.Fund.ClosingPrice * float pfr.PortfolioWeight / 100.0)
|> Seq.reduce (+)
let topPortfolioFundRecord = spsf |> Seq.head
{ PortfolioId = p; Price = price; TradedOn = topPortfolioFundRecord.Fund.TradedOn }
)
(tradedOnKey, pricedPortfoliosGroup)
)
which requests the following warning:
Lint: Seq.map f (Seq.map g x)may be reorganized into Seq.map (g >>f) x.
I believe the warning is triggered by these two top maps:
|> Seq.map (fun (tradedOnKey : DateTime, portfolioSeq : seq<PortfolioId * seq<PortfolioFundRecord>>) ->
let pricedPortfoliosGroup =
portfolioSeq
|> Seq.map (fun (p : PortfolioId, spsf : (seq<PortfolioFundRecord>)) ->
but I don’t know how to reorganize them, since my second parameter is a sequence, and I want to “convert it”, but not smooth it.
Can you suggest a way to do this? I was also looking for a way to turn off the warning about the appearance of only this piece of code, but the power tools do not seem to offer a way to do this.
Here's the whole function for completeness:
let getPortfoliosPrices(dbFundsWithPortfolioFunds : (DbFunds * DbPortfolioFunds) Linq.IQueryable)(takenDays: int) =
let portfolioPrices =
dbFundsWithPortfolioFunds
|> Seq.collect(fun (f : DbFunds, fp : DbPortfolioFunds) ->
takenDays |> getStockPrices f.Symbol
|> Seq.map(fun(quote : FundQuote) ->
let portfolioFundRec = {PortfolioId = fp.PortfolioId; PortfolioWeight = fp.Weight; Fund = quote}
portfolioFundRec)
)
|> Seq.groupBy(fun (portfolioFundRec : PortfolioFundRecord) -> portfolioFundRec.Fund.TradedOn)
|> Seq.map(fun (tradedOnKey : DateTime, spfr : PortfolioFundRecord seq) ->
let gpfr = spfr |> Seq.groupBy (fun(pfr : PortfolioFundRecord)->pfr.PortfolioId)
(tradedOnKey, gpfr)
)
|> Seq.map (fun (tradedOnKey : DateTime, portfolioSeq : seq<PortfolioId * seq<PortfolioFundRecord>>) ->
let pricedPortfoliosGroup =
portfolioSeq
|> Seq.map (fun (p : PortfolioId, spsf : (seq<PortfolioFundRecord>)) ->
let price =
spsf
|> Seq.map (fun (pfr : PortfolioFundRecord) -> pfr.Fund.ClosingPrice * float pfr.PortfolioWeight / 100.0)
|> Seq.reduce (+)
let topPortfolioFundRecord = spsf |> Seq.head
{ PortfolioId = p; Price = price; TradedOn = topPortfolioFundRecord.Fund.TradedOn }
)
(tradedOnKey, pricedPortfoliosGroup)
)
portfolioPrices