F# 和 ADO.NET - 惯用的 F#
我刚刚开始学习 F#。我昨晚写了这段 F#/ADO.NET 代码。 您将通过哪些方式改进语法 - 使其感觉像惯用的 F#?
let cn = new OleDbConnection(cnstr)
let sql = "SELECT * FROM People"
let da = new OleDbDataAdapter(new OleDbCommand(sql, cn))
let ds = new DataSet()
cn.Open()
let i = da.Fill(ds)
let rowCol = ds.Tables.[0].Rows
let rowCount = rowCol.Count
printfn "%A" rowCount
for i in 0 .. (rowCount - 1) do
let row:DataRow = rowCol.[i]
printfn "%A" row.["LastName"]
注意:我确实发现语法检查器不喜欢 rowCol.[i].["姓氏"] 处理双索引器的正确方法是什么?我必须将代码分成两行。
另外如果我没有沿着 DataSet 路线并使用 SqlDataReader 将其数据加载到 F# 记录中。我应该使用什么集合结构来包含记录? 标准 .NET 列表<>?
I'm just starting to learn F#. I wrote this F#/ADO.NET code last night.
In what ways would you improve the syntax - make it feel like idiomatic F#?
let cn = new OleDbConnection(cnstr)
let sql = "SELECT * FROM People"
let da = new OleDbDataAdapter(new OleDbCommand(sql, cn))
let ds = new DataSet()
cn.Open()
let i = da.Fill(ds)
let rowCol = ds.Tables.[0].Rows
let rowCount = rowCol.Count
printfn "%A" rowCount
for i in 0 .. (rowCount - 1) do
let row:DataRow = rowCol.[i]
printfn "%A" row.["LastName"]
Note: I did find the syntax checker did not like
rowCol.[i].["LastName"]
What is the proper way to handle dual-indexers? I had to break up the code over two lines.
Also If I hadn't gone down the DataSet route and used a SqlDataReader that loaded its data into F# records. What collection structure should I use for containing the records?
The standard .NET List<>?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码的关键部分涉及不起作用的 .NET API,因此无法使这部分代码更加惯用或更好。然而,函数式编程的关键是抽象,因此您可以将这些(丑陋的)代码隐藏到一些惯用且可重用的函数中。
为了在 F# 中表示数据集合,您可以使用标准 F# 列表类型(这有利于功能数据处理)或
seq<'a>
(这是标准 .NETIEnumerable<') a>
在封面下),在与其他 .NET 库一起使用时效果很好。根据您在代码中其他地方访问数据库的方式,以下内容可能有效:
现在您可以使用
query
函数大致如下编写原始代码:您可以编写的另一个示例是:
顺便说一句:关于Mau 的建议 如果有更直接的方法使用
for
等语言结构编写代码,我不会过度使用高阶函数。上面的iter
示例很简单,有些人会发现它更具可读性,但没有通用规则......The key part of your code deals with .NET API that is not functional, so there is no way to make this part of the code particularly more idiomatic or nicer. However, the key thing in functional programming is abstraction, so you can hide this (ugly) code into some idiomatic and reusable function.
For representing collections of data in F#, you can either use standard F# list type (which is good for functional data processing) or
seq<'a>
(which is standard .NETIEnumerable<'a>
under the cover), which works nicely when working with other .NET libraries.Depending on how you access database elsewhere in your code, the following could work:
Now you can use the
query
function to write your original code roughly like this:Another example you could write is:
BTW: Regarding the suggestion by Mau I wouldn't use higher-order functions excessively if there is a more direct way to write the code using language constructs such as
for
. The example withiter
above is simple enough and some people will find it more readable, but there is no general rule...我为 F# 编写了一个 ADO.NET 功能包装。使用这个库,您的示例如下所示:
I wrote a functional wrapper over ADO.NET for F#. With this library your example looks like this:
嗯,一开始你没有什么可以改变的,但是每当你处理数据集合(比如最后几行)时,你都可以使用内置的 Seq、List、Array 函数。
=
Well, there's not much you can change in the first bit but whenever you're processing collections of data like in the last few rows, you can use the built-in Seq, List, Array functions.
=