LinqPad 4 中缺少 Observable 引用?

发布于 2024-09-30 11:22:54 字数 1683 浏览 8 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-10-07 11:22:54

我找到了。 LinqPad 中的 F4 会弹出一个“添加引用”对话框,从那里一切正常。

I found it. F4 in LinqPad brings up an Add-reference dialog, and everything works fine from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文