Best approach to group CSV rows?


#1

Working further on my expenses CSV file.
I get a Seq of values of this type

type TaggedExpense = { Date : DateTime ; Category: string ; Amount: double ; Currency: string ; Note: string; Tags: string list}

What is the best approach to get the total amount of the expenses grouped per date?
A fold, adding a new element in the accumulator when a new date is encountered? (knowing that expenses are ordered chronologically) Or is there a better way?

Thanks in advance!


#2

The idea to fold over the list and accumulate results in an associative container sounds solid.

But the most straightforward solution would be to just use Seq.groupBy followed by Seq.sumBy:

(* get your sequence of records here *)
|> Seq.groupBy (fun { Date = it } -> it)
|> Seq.map (fun (date, it) ->
  date, (it |> Seq.sumBy (fun { Amount = it } -> it))
)

#3

Thanks. It feels as if using fold, I would have reimplemented those functions I didn’t know of!