Group totals in F# - easy with sequences, is it possible with lists?

后端 未结 3 1997

Given a sequence of a group id/value tuples, it was easy to calculate group totals (pretty much the same way I would do it with C# and LINQ):

let items = [\"g1\"         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-09 05:30

    While there's nothing wrong with gradbot's solution, I'd just keep it simple and use Seq.toList to convert sequences back to lists when desired. So you could rewrite your definition as:

    let groupsums =
        items
        |> Seq.groupBy fst
        |> Seq.toList
        |> List.map (fun (_,s) -> Seq.sumBy snd s)
    

提交回复
热议问题