gpt4 book ai didi

f# - 如何从记录转换为元组?

转载 作者:行者123 更新时间:2023-12-04 20:06:55 25 4
gpt4 key购买 nike

我经常使用 CSV 类型提供程序,它对所有基础数​​据使用元组。我的问题是,我遇到了包含很多列的 csv 文件,发现围绕操作这种大小的数据构建基础设施相当麻烦。我如何构建一个只更新列的特定子集而不必创建巨大的模式匹配语句的函数?请参见下面的代码示例。

// I want to generate lots of sample data for the first three columns
// And I would like to avoid creating giant record types in order to
// do this task as the csv's are always changing and the records would
// have to change as well
type CumbersomeCSVRow = // 17 fields now, but imagine 212
string * string * string * Option<int> * string *
Option<int> * string * string * string * string *
string * string * Option<float> * string *
Option<float> * Option<float> * string

// Here is a sample row of data
let sampleCumbersomeRow : CumbersomeCSVRow =
("First","Second","Third",Some(52),"MSCI",None,"B74A123","",
"Airlines","Transportation","","",Some(1.04293),"Updated",
Some(0.95),Some(56.7423),"Measured")

// Record type of the sample data that I want to 'insert' into the cumbersome data type
type FirstThreeStrings =
{ First : string; Second : string; Third : string}

// and some instances of the new data
let generatedFrontMatters =
// imagine lots of sample data and workflows to create it, hence the records
seq { for letter in ["A";"B";"C"] ->
{ First = letter;
Second = letter + letter
Third = letter + letter + letter } }

最佳答案

看看FSharp.Reflection模块

// Your Code --------------------------------------------------

// I want to generate lots of sample data for the first three columns
// And I would like to avoid creating giant record types in order to
// do this task as the csv's are always changing and the records would
// have to change as well
type CumbersomeCSVRow = // 17 fields now, but imagine 212
string * string * string * Option<int> * string *
Option<int> * string * string * string * string *
string * string * Option<float> * string *
Option<float> * Option<float> * string

// Here is a sample row of data
let sampleCumbersomeRow : CumbersomeCSVRow =
("First","Second","Third",Some(52),"MSCI",None,"B74A123","",
"Airlines","Transportation","","",Some(1.04293),"Updated",
Some(0.95),Some(56.7423),"Measured")

// Record type of the sample data that I want to 'insert' into the cumbersome data type
type FirstThreeStrings =
{ First : string; Second : string; Third : string}

// and some instances of the new data
let generatedFrontMatters =
// imagine lots of sample data and workflows to create it, hence the records
seq { for letter in ["A";"B";"C"] ->
{ First = letter;
Second = letter + letter
Third = letter + letter + letter } }

// Response ---------------------------------------------------

open FSharp.Reflection

// create a static back matter to append to
// the slicing here is the part that will need to change as the data changes
// ++ maybe there's a better way to handle this part??
let staticBackMatter =
sampleCumbersomeRow
|> FSharpValue.GetTupleFields
|> (fun x -> x.[3 .. 16])

// cast the front matter using FSharp.Reflection
let castedFrontMatters = Seq.map FSharpValue.GetRecordFields generatedFrontMatters

// append the data arrays together, create a tuple using reflection, and downcast the result
// to your type
let generatedRows =
castedFrontMatters
|> Seq.map (fun frontArray -> Array.append frontArray staticBackMatter)
|> Seq.map (fun data -> FSharpValue.MakeTuple(data,typeof<CumbersomeCSVRow>) :?> CumbersomeCSVRow)
|> Seq.toList

我不知道这是否是正确的 F#。

关于f# - 如何从记录转换为元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50614975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com