I think I have a good clean solution for you.
First, go back to using Func<int, int> - you can easily turn it into Func<int, IObservable<int>> using Observable.FromAsyncPattern .
I used this for testing:
Func<int, int> mkInt = ts => { Thread.Sleep(100); return ts + 1; };
Now here is the money maker:
Func<int, Func<int, int>, IObservable<int>> mkInts = (i0, fn) => Observable.Create<int>(o => { var ofn = Observable .FromAsyncPattern<int, int>( fn.BeginInvoke, fn.EndInvoke); var s = new Subject<int>(); var q = s.Select(x => ofn(x)).Switch(); var r = new CompositeDisposable(new IDisposable[] { q.Subscribe(s), s.Subscribe(o), }); s.OnNext(i0); return r; });
The iterating function turns into an asynchronous observable.
The variable q supplies values ββfrom the subject to the observed iterative function and selects the calculated observable. The Switch method equalizes the result and ensures that every call to the observed iterative function is correctly cleared.
In addition, using CompositeDisposable allows both subscriptions to be deleted as one. Very neat!
It is easily used as follows:
using (mkInts(7, mkInt).Subscribe(Console.WriteLine)) { Console.ReadLine(); }
You now have a fully parameterized version of your generator function. Nice, huh?