Czy ktoś wie o odpowiednim zamienniku dla tego handmade jeśli/then/else operator dla reaktywnych rozszerzeń (.Net/C#)?Czy istnieje operator if/then/else dla obserwowalnych w języku C# dostępny?
public static IObservable<TResult> If<TSource, TResult>(
this IObservable<TSource> source,
Func<TSource, bool> predicate,
Func<TSource, IObservable<TResult>> thenSource,
Func<TSource, IObservable<TResult>> elseSource)
{
return source
.SelectMany(
value => predicate(value)
? thenSource(value)
: elseSource(value));
}
Przykład użycia (zakładając numbers
jest typu IObservable<int>
:
numbers.If(
predicate: i => i % 2 == 0,
thenSource: i => Observable
.Return(i)
.Do(_ => { /* some side effects */ })
.Delay(TimeSpan.FromSeconds(1)), // some other operations
elseSource: i => Observable
.Return(i)
.Do(_ => { /* some other side effects */ }));
Podoba Ci się to? https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/if.md ... opps, to jest JS przepraszam, a nie C#. – AndyJ
Oto wersja C#: https://github.com/React-Extensions/Rx.NET/blob/develop/Rx.NET/Source/src/System.Reactive/Linq/Observable/If.cs – AndyJ
Tak, ale to jeden to 'wewnętrzny'. –