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

后端 未结 3 509
旧巷少年郎
旧巷少年郎 2021-02-09 04:50

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:40

    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)
    

提交回复
热议问题