Seq.generate 未定义 - 这是否已弃用?

发布于 2024-10-30 01:17:32 字数 673 浏览 2 评论 0原文

我正在尝试测试 Beginning F# 书中的一些 F# 代码,但在引用“Seq.generate”时出现“未定义”错误。 (我正在使用 VS2010 - 所以这可能适用于 VS/F# 的早期 vn)

FWIW 我还安装了 F# Powerpack dll,但这似乎没有什么区别。

这里有解决方法/替代方法吗?

/// execute a command using the Seq.generate
let execCommand (connName: string) (cmdString: string) =
    Seq.generate
        // This function gets called to open a connection and create a reader
        (fun () -> openConnectionReader connName cmdString)
        // This function gets called to read a single item in
        // the enumerable for a reader/connection pair
        (fun reader -> readOneRow(reader))
        (fun reader -> reader.Dispose())

I'm trying to test some F# code from the Beginning F# book but am getting 'not defined' error with the reference to 'Seq.generate'. (I'm using VS2010 - so this may have worked with earlier vn of VS/F#)

FWIW I've also installed the F# Powerpack dll, but that does not seem to make a difference.

Is there a workaround/alternative here?

/// execute a command using the Seq.generate
let execCommand (connName: string) (cmdString: string) =
    Seq.generate
        // This function gets called to open a connection and create a reader
        (fun () -> openConnectionReader connName cmdString)
        // This function gets called to read a single item in
        // the enumerable for a reader/connection pair
        (fun reader -> readOneRow(reader))
        (fun reader -> reader.Dispose())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2024-11-06 01:17:32

您绝对可以使用 F# PowerPack 中的 Seq.generate (通过引用它,或者更简单地通过从源代码复制它)。我认为该函数已被删除,因为您现在可以使用 use 关键字处理对象的处理。

使用 readOneRow 函数编写此代码需要使用可变状态,但您可能可以使用如下方式重写它:

/// execute a command using the Seq.generate
let execCommand (connName: string) (cmdString: string) =
  seq { use reader = openConnectionReader connName cmdString
        while reader.ReadNext() do 
            // Read some information from the reader (as done in 'readOneRow')
            let a = reader.GetInt32(0) // (e.g. ..or something like that)
            yield a  // generate next element of the sequence
      }

use 关键字正确处理读取器,这样就完成了自动为您服务。 Seq.generate 函数非常好,但我认为在大多数情况下显式编写相同的内容可能更具可读性。 (特别是如果您没有在其他地方使用 readOneRow)。

You can definitely use Seq.generate from the F# PowerPack (either by referencing it, or more easily just by copying it from the source). I think that the function was removed because you can now handle disposal of objects using the use keyword.

Writing this using the readOneRow function would require using mutable state, but you can probably rewrite it by using something like this:

/// execute a command using the Seq.generate
let execCommand (connName: string) (cmdString: string) =
  seq { use reader = openConnectionReader connName cmdString
        while reader.ReadNext() do 
            // Read some information from the reader (as done in 'readOneRow')
            let a = reader.GetInt32(0) // (e.g. ..or something like that)
            yield a  // generate next element of the sequence
      }

The use keyword properly disposes of the reader, so that's done automatically for you. The Seq.generate function is quite nice, but I think that writing the same thing explicitly is probably more readable most of the time. (Especially if you're not using readOneRow anywhere else).

相权↑美人 2024-11-06 01:17:32

生成可以在FSharp.PowerPack.Compatibility中找到:Compat .Seq.fs

generate can be found in FSharp.PowerPack.Compatibility:Compat.Seq.fs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文