我在处理无限的ienumerable ,但我没有t看到一个很好的答案,以测试枚举是否是无限的。
我的最终目标是为.NET中的所有收集类型编写一个函数,但要安全地穿越整个集合,而不会因某些无限收藏而出现长时间等待时间的风险。
OPT1:我认为我可能能够分辨出枚举是否是无限的,并且抛出了例外。但是,我认为这将是.NET聚合LINQ表达式(例如Max,Count)的方法。
Opt2:是否有其他一些抽象支持大多数所有枚举类型,但是不允许无限元素? iCollection
看起来很有希望,支持列表和数组,但不排队或许多自定义Ienumerables。
我也很难确定偶像是否可以无限。
建议?
I saw the post on ways to handle an infinite IEnumerable, but I didn't see a good answer for testing if an enumerable is infinite.
My end goal is to write a function for all collection types in .NET, but to safely traverse the whole collection without the risk of long wait times due to some infinite collection.
OPT1: I thought I might be able tell if an enumerable is infinite and throw an exception. However, I figure this would be the approach for .NET aggregate LINQ expressions (e.g. Max, Count) if it were possible.
- EDIT: As suspected, another question was suggested that directly declares this option impossible
OPT2: Is there some other abstraction that supports most all enumerable types, but disallows infinite elements? ICollection
looks promising, supports lists and arrays, but not Queues or many custom IEnumerables.
I'm also having a hard time determining if ICollection can be infinite or not.
Suggestions?
发布评论
评论(1)
您实际上是遇到停止问题。没有通用算法可以用于确定图灵完整计算是否会完成。
您可以拥有
iEnumerable< int
,该列举了π中的所有数字。无法确定该功能是否一直在继续进行(根据数学,或者应像数学一样),或者仅估计π到十亿个小数。我通常使用
IREADONLYCOLLECTION
仅出于这个目的。没什么,不过带有额外的 int
具有 maxValue 。也就是说,恶意实施者可以通过仅说谎并设置
count
to,例如0
,,可以轻率地实现ireadonLyCollection< t>
作为无限序列因此没有保证。但是,由于任何任何 c#方法都可以假设地阻止,因此我认为这是该课程的标准。我首先意识到
ireadonLyCollection代码>在2013年
,此后不久就开始使用它。有时使用它仍然有些尴尬,因为许多其他API返回
iEnumerable< t>
。您可以轻松地将它们转换为IREADONLYCOLLECTION< t>
,都可以使用 toarray 或有时仍然有点不方便。尽管如此,我一直曾经由递延执行执行足够的时间我强烈偏爱
iReadonLyCollection< t>
。You are literally running into the Halting Problem. There's no general-purpose algorithm that can be used to determine whether or not a Turing-complete computation will ever complete.
You could have an
IEnumerable<int>
that enumerates all the digits in π. There's no way of telling if that function just keeps on going (as it should, according to mathematics), or only approximates π to a billion decimals.I usually use
IReadOnlyCollection<T>
for just that purpose. It's nothing butIEnumerable<T>
with an extra Count property. That property strongly suggests that the collection has a finite length - particularly becauseint
has a MaxValue.That said, a malicious implementer could trivially implement
IReadOnlyCollection<T>
as an infinite sequence by just lying and settingCount
to, say,0
, so there are no guarantees. But since any C# method could hypothetically block forever, I consider that par for the course.I first became aware of
IReadOnlyCollection<T>
in 2013 and started using it soon thereafter. Using it is still a bit awkward at times, since so many other APIs returnIEnumerable<T>
. You can easily convert them toIReadOnlyCollection<T>
with either ToArray or ToList, but it's still a little inconvenient at times.Still, I've been bitten by deferred execution enough times that I strongly favour
IReadOnlyCollection<T>
.