Có ai biết thay thế thích hợp cho số thủ công nếu/sau đó/người điều hành khác cho tiện ích mở rộng phản hồi (.Net/C#) không?Có một toán tử if/then/else cho các quan sát trong C# có sẵn không?
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));
}
Cách sử dụng ví dụ (giả sử numbers
là loại 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 */ }));
Như thế này? https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/if.md ... opps, đó là JS xin lỗi, thay vì C#. – AndyJ
Đây là phiên bản C#: https://github.com/Reactive-Extensions/Rx.NET/blob/develop/Rx.NET/Source/src/System.Reactive/Linq/Observable/If.cs – AndyJ
Có, nhưng điều này một là 'nội bộ'. –