1

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?

4

1 に答える 1

1

見つけた。LinqPad で F4 キーを押すと、[参照の追加] ダイアログが表示され、そこからすべてが正常に機能します。

于 2010-11-02T17:25:23.970 に答える