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

时间:2011-03-31 20:22:18

标签: f# sequence

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

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())

2 个答案:

答案 0 :(得分:4)

您绝对可以使用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)。

答案 1 :(得分:0)

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