用于连续半部分的无限序列的 Linq 语句
给定一个起始数,想象一下它的连续两半的无限序列。
1, 0.5, 0.25, 0.125, ...
(忽略 double 固有的任何数值不稳定性。)
是否可以在单个表达式中完成此操作,而无需编写任何自定义扩展方法或生成器方法?
Given a starting number, imagine an infinite sequence of its successive halves.
1, 0.5, 0.25, 0.125, ...
(Ignore any numerical instabilities inherent in double
.)
Can this be done in a single expression without writing any custom extension methods or generator methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道单表达式方式,但我在这里找到了这个聪明的生成器代码: http:// /csharpindepth.com/articles/Chapter11/StreamingAndIterators.aspx
在您的情况下,您将使用它:
I don't know of a single-expression way but I found this clever generator code here: http://csharpindepth.com/articles/Chapter11/StreamingAndIterators.aspx
In your case you'd use it:
为了好玩,这里有一个在单个表达式中创建真正的无限序列的技巧。前两个定义是类字段,因此它们不需要初始化表达式。
For fun, here's a trick to create a real infinite sequence in a single expression. The first two definitions are class fields, so that they do not require an expression to be initialised.
这是类似于 @hvd 提供的答案,但使用
Y
运算符定义 此处,这消除了对局部变量的需要:一个示例用法是:
它将输出 20, 10, 5, 2.5 等...
我不建议在生产代码中使用它,但它很有趣。
Y 运算符还允许其他递归 lambda 表达式,例如:
Here is an answer similar to the one @hvd provided, but using the
Y
operator defined here, this removes the need for the local variables:An example use would be:
Which would output 20, 10, 5, 2.5 etc...
I wouldn't advise using this in production code but it is fun.
The
Y
operator also allows other recursive lambda expressions, e.g:它实际上并不是无限的,但由于
Repeat
和Select
都使用延迟执行,因此您不会损失任何性能。不知道任何创建无限 linq 表达式的本机方法。
或者您可以手动编写无限版本的
.Repeat
It isn't actually infinite, but as both
Repeat
andSelect
use deferred execution, you won't lose any performance.Don't know any native way to create infinite linq expression.
Or you can manually write infinite version of
.Repeat
我不知道有什么方法可以直接使用 LINQ 来创建无限序列。但您可以制作一个非常长的序列。
但是,由于
double
的精度有限,因此在n
变得太高后,您只会得到零。I don't know of any way to make an infinite sequence with straight LINQ. But you could make a very long sequence.
However, since
double
has finite precision, you'll get nothing but zeroes aftern
gets too high.