5

我在 F# 中为某些客户端数据定义了一个记录类型,如下所示:-

  type DataPoint = {
       date: string; 
       dr: string; 
       Group: string; 
       Product: string; 
       Book: int; 
       Revenue: int} with 
          static member fromFile file =
               file
               |> File.ReadLines
               |> Seq.skip 1 //skip the header
               |> Seq.map (fun s-> s.Split ',') // split each line into array
               |> Seq.map (fun a -> {date = string a.[0]; dr = string a.[1];
                              Group = string a.[2]; Product = string a.[3];
                                Book = int a.[4]; Revenue = int a.[5] });;  

    // creates a record for each line
    let pivot (file) = DataPoint.fromFile file
              |> ??????????

对于 date、dr、Group 和 Product 都相等的行,我想将所有 Book 和 Revenue 条目相加,生成一个旋转的行。所以某种 if else 语句应该没问题。我怀疑我需要从第一个数据点开始并递归地添加每个匹配的行,然后删除匹配的行以避免输出中的重复。

完成此操作后,我将能够轻松地将这些旋转的行写入另一个 csv 文件。

任何人都可以让我开始吗?

4

2 回答 2

7

Seq.groupBySeq.reduce是您正在寻找的:

let pivot file = 
    DataPoint.fromFile file
    |> Seq.groupBy (fun dp -> dp.date, dp.dr, dp.Group, dp.Product)
    |> Seq.map (snd >> Seq.reduce (fun acc dp -> 
                          { date = acc.date; dr = acc.dr; 
                            Group = acc.Group; Product = acc.Product;
                            Book = acc.Book + dp.Book; 
                            Revenue = acc.Revenue + dp.Revenue; }))
于 2012-11-20T13:36:40.710 回答
3

快速破解,应该给你一些想法:

// Sample data
let data = [
             {date    = "2012-01-01"
              dr      = "Test"
              Group   = "A" 
              Product = "B"
              Book    = 123
              Revenue = 123}
             {date   = "2012-01-01"
              dr      = "Test"
              Group   = "A"
              Product = "B"
              Book    = 123
              Revenue = 123}
             {date = "2012-01-01"
              dr = "Test"
              Group = "B" 
              Product = "B"
              Book = 11
              Revenue = 123}]


let grouped = data |> Seq.groupBy(fun d -> (d.date, d.dr, d.Group, d.Product))
                   |> Seq.map (fun (k,v) -> (k, v |> Seq.sumBy (fun v -> v.Book), v |> Seq.sumBy (fun v -> v.Revenue)))

for g,books,revs in grouped do
   printfn "Books %A: %d" g books
   printfn "Revenues %A: %d" g revs

印刷

Books ("2012-01-01", "Test", "A", "B"): 246
Revenues ("2012-01-01", "Test", "A", "B"): 246
Books ("2012-01-01", "Test", "B", "B"): 11
Revenues ("2012-01-01", "Test", "B", "B"): 11
于 2012-11-20T13:40:43.223 回答