This is not real life example (and this code will probably not compile) but I'm trying to make it a little bit simpler than the problem I actually have.
Let's say I have collection of images:
private void IEnumerable<Image> GetImages()
{
foreach (var filename in GetFilenames())
{
yield return Image.LoadFile(filename);
}
}
and I would like to show slideshow driven by user pressing 'space':
var images = Observable.FromEvent(form, "KeyPress")
.Where(e => e.KeyCode == KeyCode.Space)
.Zip(GetImages.ToObservable(), (k, i) => i);
And this kind of works. It does emit next image when space is pressed. The problem is it actually loads them at full speed, so they get buffered and consume a lot of memory (and processing power when loading). I could feed filtered-key-presses into GetImages and to the zipping there but I would not preserve purity of GetImages.
Is there any way to prevent enumerable.ToObservable() from being enumerated in advance if not needed?
Another example (this one will compile):
var observable =
Observable.Interval(TimeSpan.FromSeconds(1))
.Zip(
Observable.Range(0, 1000000).Do(x => Console.WriteLine("produced {0}", x)),
(_, v) => v
);
var subscription = observable.Subscribe(x => Console.WriteLine("consumed {0}", x));
Console.WriteLine("Press <enter>...");
Console.ReadLine();
It will generate a lot of "produced" (in advance) but only one "consumed" per second.