如何重构以避免F#Lint的警告?

时间:2017-02-15 11:26:37

标签: f#

我在一个编译和有效工作的函数中有以下嵌套的Seq.map()调用:

|> 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)
)

会提示以下lint警告:

  

Lint:Seq.map f (Seq.map g x)可能会被重构为Seq.map (g >>f) x

我相信这些前2个地图调用会提示警告:

|> Seq.map (fun (tradedOnKey : DateTime, portfolioSeq : seq<PortfolioId * seq<PortfolioFundRecord>>) ->
        let pricedPortfoliosGroup = 
            portfolioSeq 
            |> Seq.map (fun (p : PortfolioId, spsf : (seq<PortfolioFundRecord>)) ->

但是我不知道如何重构它们,因为我的第二个参数是一个序列,我想&#34;转换它&#34;但不要压扁它。

你能建议一种方法吗?我还想找到一种方法来关闭针对该代码片段的lint警告,但电动工具似乎没有提供这样做的方法。

以下是完整性的全部功能:

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

1 个答案:

答案 0 :(得分:5)

在您的第二个顶级Seq.map中,您只是通过tradedOnKey,并对您的序列执行某些操作。您可以通过执行以下操作将所有内容合并为一个:

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-> portfolioFundRec.Fund.TradedOn)
    |> Seq.map(fun (tradedOnKey, spfr) -> 
        let gpfr = spfr |> Seq.groupBy (fun pfr -> pfr.PortfolioId)
        let pricedPortfoliosGroup = 
            gpfr
            |> Seq.map (fun (p, spsf) ->
                let price = 
                    spsf 
                    |> Seq.sumBy (fun pfr -> pfr.Fund.ClosingPrice * float pfr.PortfolioWeight / 100.0) 
                let topPortfolioFundRecord = spsf |> Seq.head
                { PortfolioId = p; Price = price; TradedOn = topPortfolioFundRecord.Fund.TradedOn }
            )
        (tradedOnKey, pricedPortfoliosGroup)
)

这应该消除警告(很难验证,因为我没有完整复制品的所有类型定义)。