我正在尝试将二维数组公开为对象上的序列序列(以便能够专门执行 Seq.fold (fun x -> Seq.fold (fun ->..) [] x) []
mytype 的内容)
下面是一个公开相同功能的玩具程序。
据我了解,这里发生了很多事情,首先是 IEnumerable
有不明确的重载,需要类型注释来显式隔离 IEnumerable
你说的是。
但是设备可能会出现问题,并且需要额外的帮助:
type blah =
class
interface int seq seq with
member self.GetEnumerator () : System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<(int*int)>> =
seq{ for i = 0 to 10 do
yield seq { for j=0 to 10 do
yield (i,j)} }
end
是否有某种方法可以使上述代码按预期工作(返回 seq<seq<int>>
)或者我是否遗漏了一些基本内容?
最佳答案
首先,GetEnumerator()
应该返回 IEnumerator<T>
不是IEnumerable<T>
...
这将使您的示例代码进行编译。
type blah =
interface seq<seq<(int * int)>> with
member self.GetEnumerator () =
(seq { for i = 0 to 10 do
yield seq { for j=0 to 10 do
yield (i,j)} }).GetEnumerator()
interface System.Collections.IEnumerable with
member self.GetEnumerator () =
(self :> seq<seq<(int * int)>>).GetEnumerator() :> System.Collections.IEnumerator
关于interface - 在 F# 中实现序列的序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5600377/