Is there an if / then / else statement for observables in C #?

Does anyone know of a suitable replacement for this handmade if / then / else statement for reactive extensions (.Net / C #)?

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)); } 

Usage example (it is assumed that numbers is of type 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 */ })); 
+5
source share
1 answer

Yes, there is one: https://github.com/Reactive-Extensions/Rx.NET/blob/develop/Rx.NET/Source/src/System.Reactive/Linq/Observable/If.cs

But why not use your homemade version? It seems to work fine for me.

Sadly, as far as I know, there is no assembly for the operator for this task in .Net.

+3
source

Source: https://habr.com/ru/post/1266588/


All Articles