Where is Observable.Iterate() in Rx 1.0.10621.0/1.1.10621.0
-
22-10-2019 - |
Question
I'm finding a few examples online of Reactive Extensions that require the use of Observable.Iterate(), but the package from NuGet, Rx version 1.0.10621.0 does not seem to include it. Unless I'm doing it all wrong?
I assume that it was renamed, but I can't find any posts regarding that. Anyone know?
Solution
From Rx Forum:
Wes Dyer:
What about Observable.Iterate?
Use the Observable.Create that takes a Func, Task> which works exactly the same as Iterate except that it uses async await to drive it.
OTHER TIPS
An extension method Iterate could look like this:
public static IObservable<T> Iterate<T, TIter>([NotNull] this IObservable<TIter> source, [NotNull] T iterationStart, [NotNull] Func<TIter, T, T> iterator)
{
return Observable.Create<T>(observer =>
{
T previousElement = iterationStart;
return source.Subscribe(
value =>
{
T currentElement = iterator(value, previousElement);
observer.OnNext(currentElement);
previousElement = currentElement;
},
observer.OnError,
observer.OnCompleted
);
}).StartWith(iterationStart);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow