开发者

Missing Observable reference in LinqPad 4?

开发者 https://www.devze.com 2023-01-23 08:25 出处:网络
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 stat

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.
 // Th开发者_如何转开发e 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消