我正在尝试理解一些F#代码,但是遇到了问题。
我有一个CSV文件,它的格式如下:
CorrelationId,TagNumber,Description,CreationDate,UpdateDate,Discipline
8D3F96F3-938F-4599-BCA1-66B13199A39A,Test 70-2,Test tag - Ignore,2016-04-05 14:55:23.503,2016-04-05 14:55:23.503,Mechanical
A9FD4B9D-F7A1-4B7D-917F-D633EA0321E3,test-4,A test tag 24,2016-03-23 15:09:54.667,2016-03-30 17:35:29.553,Civil
我正在使用CSV类型提供程序进行读取
open FSharp.Data
type Tag = CsvProvider<"tags.csv">
let readTags (path:string) =
let tags = Tag.Load(path)
printfn "%i" ( tags.Rows |> Seq.length )
let tag = tags.Rows |> Seq.head
我想验证这些行,所以我借鉴了fsharpforfunandprofit铁路导向编程的方法。
type Result<'TSuccess,'TFailure> =
| Success of 'TSuccess
| Failure of 'TFailure
let bind switchFunction twoTrackInput =
match twoTrackInput with
| Success s -> switchFunction s
| Failure f -> Failure f
let validateTagName tag =
if String.length tag.TagNumber = 0 then Failure "Tag number cannot be empty"
else Success tag
let validateTagDescription tag =
if String.length tag.Description = 0 then Failure "Tag description cannot be empty"
else Success tag
但是在验证方法中我遇到了一个问题,我需要使用类型注释来注释函数。但是我不知道应该把它们注释为什么类型。我尝试创建一个新的类型并进行映射,但没有成功。
type tagType = { TagNumber: string; Description: string}
这些函数现在可以正确编译,但我只是把问题搁置了,因为我现在不确定如何从Tag.Row映射到tagType。理想情况下,我希望在不进行任何映射的情况下进行验证。
所有这些应该看起来怎么样?