LinqPad 4 中缺少 Observable 引用?
我在 LinqPad 4 中尝试了以下测试,并收到“Observable not in context”错误。 IEnumerable 分支可以工作,而且奇怪的是,我没有收到 IObservable 本身的错误,只是静态方法 Observable.Range 的错误。
static void Main()
{
IEnumerableSieve();
IObservableSieve();
}
private static void IEnumerableSieve()
{
IEnumerable<int> oddsFrom3 = IntsFrom(3, 2);
List<int> cache = new List<int>() { 2 };
IEnumerable<int> primes = oddsFrom3.
Where(candidate => cache.TakeWhile(prime => prime * prime <= candidate).
Where(trialDivisor => candidate % trialDivisor == 0).Count() == 0);
Console.WriteLine("IEnumerable oddsFrom3:");
foreach (int p in primes.Take(20))
{
Console.WriteLine("{0}", p);
cache.Add(p);
}
}
private static void IObservableSieve()
{
const int bigMax = 1 << 30;
// Generate the candidates as observables, cache the primes in an IEnum list as before.
// But it's TERRIBLE that the cache is now of a different TYPE than the thing it's caching.
// The cache is IEnumerable, and the thing it's caching is an IObservable.
IObservable<int> oddsFrom3 = Observable.Range(3, bigMax).Where(i => i % 2 == 1);
List<int> cache = new List<int>() { 2 };
IObservable<int> primes = oddsFrom3.
Where(candidate => cache.
TakeWhile(prime => prime * prime <= candidate).
Where(trialDivisor => candidate % trialDivisor == 0).Count() == 0);
Console.WriteLine("IObservable oddsFrom3 ");
primes.Take(20).Subscribe(p => { Console.WriteLine("{0}", p); cache.Add(p); });
}
static IEnumerable<int> IntsFrom(int i, int increment)
{
while (true)
{
yield return i;
i += increment;
}
}
有什么提示吗?
I tried the following test in LinqPad 4 and got an "Observable not in context" error. The IEnumerable branch works, and, oddly enough, I don't get an error for IObservable itself, just for the static method Observable.Range.
static void Main()
{
IEnumerableSieve();
IObservableSieve();
}
private static void IEnumerableSieve()
{
IEnumerable<int> oddsFrom3 = IntsFrom(3, 2);
List<int> cache = new List<int>() { 2 };
IEnumerable<int> primes = oddsFrom3.
Where(candidate => cache.TakeWhile(prime => prime * prime <= candidate).
Where(trialDivisor => candidate % trialDivisor == 0).Count() == 0);
Console.WriteLine("IEnumerable oddsFrom3:");
foreach (int p in primes.Take(20))
{
Console.WriteLine("{0}", p);
cache.Add(p);
}
}
private static void IObservableSieve()
{
const int bigMax = 1 << 30;
// Generate the candidates as observables, cache the primes in an IEnum list as before.
// But it's TERRIBLE that the cache is now of a different TYPE than the thing it's caching.
// The cache is IEnumerable, and the thing it's caching is an IObservable.
IObservable<int> oddsFrom3 = Observable.Range(3, bigMax).Where(i => i % 2 == 1);
List<int> cache = new List<int>() { 2 };
IObservable<int> primes = oddsFrom3.
Where(candidate => cache.
TakeWhile(prime => prime * prime <= candidate).
Where(trialDivisor => candidate % trialDivisor == 0).Count() == 0);
Console.WriteLine("IObservable oddsFrom3 ");
primes.Take(20).Subscribe(p => { Console.WriteLine("{0}", p); cache.Add(p); });
}
static IEnumerable<int> IntsFrom(int i, int increment)
{
while (true)
{
yield return i;
i += increment;
}
}
any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了。 LinqPad 中的 F4 会弹出一个“添加引用”对话框,从那里一切正常。
I found it. F4 in LinqPad brings up an Add-reference dialog, and everything works fine from there.