Home > other >  Scan operator with a result selector
Scan operator with a result selector

Time:11-01

In Rx.NET, Aggregate has an overload that accepts a resultSelector, but Scan does not. Is there a reason for this? And how would I go about creating a Scan operator that does accept a result selector, i.e., with signature: IObservable<TResult> Scan<TSource,TAccumulate,TResult>(this IObservable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> accumulator, Func<TAccumulate,TResult> resultSelector).

CodePudding user response:

Is the trivial implementation below sufficient?

public static IObservable<TResult> Scan<TSource, TAccumulate, TResult>(
    this IObservable<TSource> source,
    TAccumulate seed,
    Func<TAccumulate, TSource, TAccumulate> accumulator,
    Func<TAccumulate, TResult> resultSelector)
{
    return source.Scan(seed, accumulator).Select(resultSelector);
}
  • Related