简体   繁体   English

无限枚举是否仍然是“可枚举的”?

[英]Is an infinite enumerable still “enumerable”?

Like two overlapping line segments, we can find infinite points of intersection. 像两个重叠的线段一样,我们可以找到无限的交点。 To enumerate all these points might not make sense, and we might just want to present that this collection is infinity. 枚举所有这些点可能没有意义,我们可能只想提出这个集合是无限的。

Floating point numbers have defined NegativeInfinity and PositiveInfinity . 浮点数已定义NegativeInfinityPositiveInfinity A number which represents count or ordinal seem not necessary to use floating point numbers, however, integers are not defined something to represent infinity. 表示计数序数的数字似乎不需要使用浮点数,但是,没有定义整数来表示无穷大。

So I tried to implement an infinite enumerable. 因此,我尝试实现一个无限的枚举。 But I suddenly get confused with the term "enumerable" .. 但是我突然对“可枚举”一词感到困惑。

Is there a better way to solve this problem? 有解决这个问题的更好方法吗? And is an infinite enumerable still enumerable ? 无限枚举是否仍然可以枚举

  • Code

     public partial class Infinity: IEnumerable<object> { IEnumerator<object> IEnumerable<object>.GetEnumerator() { for(; ; ) yield return Infinity.Enumerable; } public IEnumerator GetEnumerator() { for(; ; ) yield return Infinity.Enumerable; } public Infinity LongCount( Func<object, bool> predicate=default(Func<object, bool>)) { return Infinity.Enumerable; } public Infinity Count( Func<object, bool> predicate=default(Func<object, bool>)) { return Infinity.Enumerable; } public static readonly Infinity Enumerable=new Infinity(); } 

Edit: 编辑:

Thanks for answering. 谢谢回答。 I'm not confused with IEnumerable and IEnumerator . 我对IEnumerableIEnumerator 感到困惑。 GetEnumerator methods return Infinity.Enumerable is because I do not want to declare an extra dummy object such as: GetEnumerator方法返回Infinity.Enumerable是因为我不想宣布额外的虚拟对象,如:

static readonly object dummy=new object();

and yield return dummy in GetEnumerator methods. 并在GetEnumerator方法中产生yield return dummy

And is an infinite enumerable still enumerable? 无限枚举是否仍然可以枚举?

Enumerable, in this sense, is based off the second definition of enumerate : 在这种意义上,可枚举基于枚举的第二个定义

to specify one after another 指定一个接一个

It is not referring to the (more common outside of computing) definition whereby it effectively means "able to be counted." 它不是指(在计算之外更常见的)定义,它实际上表示“可以计算”。

In this sense, an infinite series can definitely be listed one item after another, and qualifies as an enumerable. 从这个意义上说,一个无限系列肯定可以一个接一个地列出,并且可以被枚举。

That being said, I don't see the purpose behind your code in this example. 话虽如此,在这个示例中,我看不到您的代码背后的目的。 Infinite enumerables are typically representing something like a stream of data without an end, or other sources where there is no "end", but the potential to continually pull information. 无限枚举通常表示诸如无止境的数据流之类的东西,或者表示没有“终结”但具有持续拉动信息潜力的其他来源。

The positive natural numbers are infinite and clearly enumerable (1, 2, 3, …). 正自然数是无限的,并且显然可以枚举(1、2、3,……)。 The concept is well-defined even outside of C#. 即使在C#之外,该概念也是定义明确的。

Your class however has problems because you are confusing the IEnumerable and IEnumerator interface. 但是,您的类遇到了问题,因为您将IEnumerableIEnumerator接口混淆了。 The GetEnumerator method returns only one enumerator. GetEnumerator方法仅返回一个枚举数。 That is infinite. 是无限的。

An easy implementation of an infinite IEnumerable in C# (as a method rather than a class) looks as follows: 在C#中轻松实现无限IEnumerable (作为方法而不是类)如下所示:

IEnumerable<int> Infinite() {
    int i = 1;
    while (true)
        yield return i++;
}

Caveat: int overflows at some point. 注意: int在某些时候溢出。 However, by default C# will then simply loop back to negative numbers. 但是,默认情况下,C#随后将简单地循环回到负数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM